Getting Started with Ansible 08 – Improving your Playbook

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. In video #8, we look into a few ways we can clean up and consolidate the playbook we’ve been working with so far.

YouTube player

install_apache.yml (previous version)

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: update repository index for Ubuntu
     apt:
       update_cache: yes
     when: ansible_distribution == "Ubuntu"
 
   - name: install apache2 package for Ubuntu
     apt:
       name: apache2
       state: latest
     when: ansible_distribution == "Ubuntu"
 
   - name: add php support for apache for Ubuntu
     apt:
       name: libapache2-mod-php for CentOS
       state: latest
     when: ansible_distribution == "Ubuntu"
 
   - name: update repository index for CentOS
     dnf:
       update_cache: yes
     when: ansible_distribution == "CentOS"
 
   - name: install httpd package for CentOS
     dnf:
       name: httpd
       state: latest
     when: ansible_distribution == "CentOS"
 
   - name: add php support for apache for CentOS
     dnf:
       name: php
       state: latest
     when: ansible_distribution == "CentOS"

install_apache.yml (condensed)

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: update repository index
     apt:
       update_cache: yes
     when: ansible_distribution == "Ubuntu"
 
   - name: install apache2 and php packages for Ubuntu
     apt:
       name:
         - apache2
         - libapache2-mod-php
       state: latest
     when: ansible_distribution == "Ubuntu"
 
   - name: update repository index
     dnf:
       update_cache: yes
     when: ansible_distribution == "CentOS"
 
   - name: install apache and php packages for CentOS
     dnf:
       name:
         - httpd
         - php
       state: latest
     when: ansible_distribution == "CentOS"
   

install_apache.yml (further condensed)

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: install apache2 package
     apt:
       name:
         - apache2
         - libapache2-mod-php
       state: latest
       update_cache: yes
     when: ansible_distribution == "Ubuntu"
 
   - name: install httpd package
     dnf:
       name:
         - httpd
         - php
       state: latest
       update_cache: yes
     when: ansible_distribution == "CentOS"

install_apache.yml (condensed even futher)

 ---
 
 - hosts: all
   become: true
   tasks:
 
   - name: install apache and php
     package:
       name:
         - "Template:Apache package"
         - "Template:Php package"
       state: latest
       update_cache: yes

inventory file (with host variables added)

172.16.250.132 apache_package=apache2 php_package=libapache2-mod-php
172.16.250.133 apache_package=apache2 php_package=libapache2-mod-php
172.16.250.134 apache_package=apache2 php_package=libapache2-mod-php
172.16.250.248 apache_package=httpd php_package=php