Part 8 – Ansible Play Recap, vars, Notify, Handlers

PlayBooks are a great thing, but how are we going to handle exceptions, what about errors during the tasks? what if we want to specify a condition before executing the task? How are we going to declare variables? This post is for those things.

Play Recap

When a task fails, all the subsequent tasks will not be executed on the host. In that case, we may have to troubleshoot and rerun the tasks that failed. We cannot rerun the entire playbook for this and in some cases this is also not recommended. So to achieve this we have something called Play Recap which has the information of the failed hosts.

2017-04-04 21_53_55-vagrant@ansi_ ~_my_ansible

Notice, Ansible has created a retry file for us. Going through the contents of the retry file, it has the name of the server that failed to execute the tasks. So to rerun the task, we just need to use the ‘limit’ parameter and use the command given by recap.

$ ansible-playbook -i my_inventory my_playbook.yml --limit @/home/vagrant/my_playbook.retry

2017-04-04 22_00_39-vagrant@ansi_ ~_my_ansible


Consider a situation where you have different OS families in an inventory file which is the case most of the times and you want to run specific tasks only on certain OS family machines. We need to make use of when module for this. In our case, we have centos based machine named web which uses the package manager yum, so we want to validate this before running the yum task. Modify the playbook as below for this.

Note: To use any filters from the setup module to check the condition, gather_facts must be set to true.

2017-04-04 22_11_12-vagrant@ansi_ ~_my_ansible


We can also make use of variable declaration in YAML to avoid changes in multiple locations in the file and also for specifying the dynamic content. use the vars keywork for this. I am declaring a variable dest_file which stores the path of the destination used in the copy module. Below is the usage

2017-04-04 22_19_31-vagrant@ansi_ ~_my_ansible

Notify and Handlers

Often times, we come across requests to update the configuration file and restart the respective service for the new configuration to take effect. In such cases, we use the notify module to mention the name of the task to be done and in the handler section we mention the task itself. Bringing them all together, the playbook looks as below. The name mentioned in the notify section is case sensitive and respective handler has to be mentioned with the same name.

2017-04-04 22_41_29-vagrant@ansi_ ~_my_ansible

Final output

2017-04-04 22_43_10-vagrant@ansi_ ~_my_ansible

 Hope this was informative. Thanks!

 Part 1 – Introduction to Ansible

Part 2 – Ansible Lab Setup

Part 3 – Understanding our First Ansible Command

Part 4 – More about Inventory File

Part 5 – Ansible Config

Part 6 – Ansible Modules

Part 7 – Ansible Tasks, Plays, PlayBooks

Part 8 – Ansible Play Recap, vars, Notify, Handlers

Part 9 – Ansible Roles


#handlers, #limit, #notify, #recap, #vars, #when