Playbook with variables, templates, conditions and handlers

Conditional Execution

Sometimes you want to run a particular task from the playbook only when it meets certain criteria. It would be similar to the if else condition which we have in a programming language.
In ansible, we have “when” module to check the condition, if it returns true the task gets executed or else skipped. For example, you are writing a playbook that should run on Ubuntu and Centos systems. As we know if we want to install a package in ubuntu os, we use the apt module, for centos we use yum module. 
So, in this case, our task should check a condition of OS family and execute apt if it's Ubuntu and yum when its Centos. We can use the fact variable ansible_os_family which stores the value of OS type to check the condition. 
$ ansible -m setup web1 | grep ansible_os_family
"ansible_os_family": "Debian",  

$ cat condition.yml 
--- 
- hosts: webservers
become: yes
tasks:
- name: Install Apache on Centos
yum: name=httpd state=present
when: ansible_os_family == "RedHat"  
  
- name: Install Apache on Ubuntu
yum: name=apache2 state=present
when: ansible_os_family == "Debian"  

Templates

Templates are similar to copy module, it copies the source file to the target hosts destination. But here we don’t have plain static files, we have a template file which contains pre-defined variables. These variables could be defined in playbook or host_vars or group_vars etc.
While the template module gets executed it will read the template file and change all the variables to its value and copy the file to the target host.
Template file ends with .j2 extension which stands for Jinja2 templates. 
We use template module to copy the template file and the variable used in template file can be defined in the playbook as shown below.
$ vi web.yaml 
---
- hosts: webservers
sudo: yes
vars:
username: GreenApple 
doc_root: /var/www/html/
tasks:
- name: Copy Site Files
template: src=templates/index.j2 dest={{doc_root}}/index.html mode=0644  

A sample playbook with variables, templates, conditions and handlers.
We need a centos 6 vm node to run this code.
$ ls
ansible.cfg inventory_prod templates Vagrantfile web.yaml  
  
$ cat ansible.cfg 
[defaults]
hostfile = inventory_prod
host_key_checking = False  
  
$ cat inventory_prod 
web1 ansible_ssh_host=192.168.1.11
db1 ansible_ssh_host=192.168.1.8
[webservers]
web1
[dbservers]
db1
[datacenter:children]
webservers
dbservers
[datacenter:vars]
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant  
  
$ ls templates/
httpd.j2 index.j2  

Template for index.html file
Templates variable details from httpd.j2 template
You can get httpd.conf file after installing httpd on centos, its location is /etc/httpd/conf/httpd.conf. Copy the content of httpd.conf file into templates/httpd.j2 and replace its values as shown below.
Below mentioned variables are defined in the playbook.
$ cat templates/httpd.j2 | grep '{{'
MaxClients {{ max_clients }}
Listen {{ http_port }}
Alias {{ doc_dir }} {{ doc_root }}  

The Playbook
$ cat web.yaml 
---
- hosts: webservers
sudo: yes
vars:
http_port: 80
doc_dir: /ansible/
doc_root: /var/www/html/ansible/
max_clients: 5
ansible_python_interpreter: python 
vars_prompt:
- name: username
prompt: What is your name?
tasks:
- name: Ensure libselinux-python installed
yum: name=libselinux-python state=present
- name: Ensure Apache installed
yum: name=httpd state=present
when: ansible_os_family == "RedHat"
- name: Creates directoy
file: path=/var/www/html/ansible state=directory
- name: Ensure Apache installed
yum: name=httpd state=present
when: ansible_os_family == "RedHat"
- name: Ensure Apache is running
service: name=httpd enabled=yes state=started 

- name: Deploy configuration File
template: src=templates/httpd.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- Restart Apache
- name: Copy Site Files
template: src=templates/index.j2 dest={{doc_root}}/index.html mode=0644
- name: Stop IPTABLES Now!!
service: name=iptables state=stopped
handlers:
- name: Restart Apache
service: name=httpd state=restarted  
For more information about Visualpath, visit www.visualpath.in and follow the company on Facebook and Twitter.

Comments