Getting Started with Ansible 15 – Host Variables

Ansible is an incredible configuration management and provisioning utility that enables you to automate all the things. In this series, you’ll learn everything you need to know in order to use Ansible for your day-to-day administration duties.

YouTube player

host_vars (ubuntu web server)

 apache_package_name: apache2
 apache_service: apache2
 php_package_name: libapache2-mod-php

host_vars (ubuntu web server)

 apache_package_name: httpd
 apache_service: httpd
 php_package_name: php

main.yml (web_servers role)

 - name: install web server packages
   tags: apache,apache2,centos,httpd,ubuntu
   package:
     name:
       - "Template:Apache package name"
       - "Template:Php package name"
     state: latest
   when: ansible_distribution == "CentOS"
 
 - name: start and enable apache
   tags: apache,centos,httpd
   service:
     name: "Template:Apache service"
     state: started
     enabled: yes
 
 - name: change e-mail address for admin
   tags: apache,centos,httpd
   lineinfile:
     path: /etc/httpd/conf/httpd.conf
     regexp: '^ServerAdmin'
     line: ServerAdmin somebody@somewhere.net
   when: ansible_distribution == "CentOS"
   notify: restart_apache
 
 - name: copy html file for site
   tags: apache,apache,apache2,httpd
   copy:
     src: default_site.html
     dest: /var/www/html/index.html
     owner: root
     group: root
     mode: 0644

Handlers for the web_servers role

Create the handlers directory (within the role directory):

 mkdir handlers
 - name: restart_apache
   tags: apache,centos,httpd
   service:
     name: "Template:Apache service"
     state: restarted

Support Linux Learning