Ansible – Post – 10 – Ansible Plays and Playbooks

Ansible - Post - 10 - Ansible Plays - Playbooks
Spread the love

 

 

Ansible - Post - 10 - Ansible Plays / Playbooks :

In this blog post, we will see about plays / playbooks. Plays perform the series of action that needs to be carried out on a target​​ host, it maps the tasks to the target host, A play can contain multiple tasks and a playbook will contain multiple plays.

Plays are written in YAML, so indentation & whitespace plays a major role in writing the play, just like the python programming an incorrect indentation will lead to compilation error. Indentation should be provided at these locations,​​ 

Usually the play starts with a host declaration / global declaration followed by the credentials , variables and tasks declaration. ​​ Tasks are executed in top down order and it uses modules.

You can execute the play using the below command.

 

$ansible-playbook ​​ play.yml

 

Let’s see the different logics that can be used in Ansible, this post covers only the minimal amount of logics to write playbooks , detailed logics can be read in the ansible documentation from the RedHat ansible website.​​ 

 

include & include_vars:

include is used for including the other yaml files (plays) in your playbook, this helps you in not to reinvent the wheel again for the task you​​ have in hand, Lot of readymade playbooks are available in the ansible galaxy.​​ 

 

include_vars – helps you in getting only the variables from the yaml file.​​ 

 

 

 

register:

register is used to storing the output of task to a variable , which in turn the​​ variable can be used for another task, in the below example , we are getting the username from the shell task and then later used as the owner for the file task.

 

debug:

the debug module is used for displaying msg output during a particular execution. It​​ also uses the var parameter to display the output of a variable.

 

 

vars_prompt:

vars_prompt is used for getting the input from the user and later used in the tasks for execution. But the input you enter will not be shown in the prompt, you need to be​​ careful when you enter the input.

 

handlers:

handlers are useful in executing a task post a state is changed, in the below example the configuration file is changed which is then notified to the handler as a state change. Handlers will be executed only once.

 

 

when:

when is a conditional clause , that is used for checking a particular condition , in the below example, we see that based on the linux family it calls for the appropriate installation command.​​ 

 

Conditional clause based on output, in the below example you can see that output is registered in the result variable and if it is failed it display the message “Failure!”

 

 

templates:

 

templates uses jinja2 engine, you can insert variables from your plays into files, most commonly they are used​​ for deploying custom configurations, say that you have an static email template, you can use the jinja2 engine to insert custom variables like subject of the email , content of the email based on the execution.​​ 

 

In the below example, we can see that the configuration values in ​​ /etc/httpd/conf/httpd.conf file was replaced with the values from httpd.j2 , let see how it is done.

 

The template defines the values that needs to be changes in curly braces. So the values are substituted in the destination file.

 

 

 

Thanks for viewing the post,​​ 

please post your comments below.

Please Like & Follow us on Facebook & Twitter,

https://www.facebook.com/gadoth/

https://www.twitter.com/GadothDA/

 

Related posts

Leave a Comment