Ansible – Post – 11 – Ansible templates and variables

Ansible templates and variables
Spread the love

 

 

Ansible - Post - 11 - Ansible templates / variables :

In this blog post, we will see how to use templates and variables to get user inputs and substitute the values in the predefined templates.​​ 

I created the YAML file in my local system and copied it to the acs server.​​ At the bottom of this post, I have attached the .tar file.​​ 

 

 

Extracted the files under demo6 folder, as you can see there are 3 files and a templates folder.​​ 

 

 

 

In the configuration file, I have defined the host file which points to the inventory file.​​ 

 

 

 

 

In the inventory file, we have defined the host details as​​ seen in the previous posts.​​ 

 

 

 

Now lets see the contents of the templates directory, you can see that , it has two files httpd.j2 and index.js, they contain the variables which will receive the values from the user prompt or from the vars definition.​​ 

The variables are defined in curly braces {{ }}.​​ 

 

 

Now​​ let’s​​ look at the web_db.yaml file, ​​ you can see that vars and vars_prompt definition, under vars we have defined the values to be given for httpd.j2 and under vars_prompt , we receive user input which will then get passed on to index.j2 file.​​ 

 

You can also the notify and handlers keywords, Notify triggers an event post the completion of the previous event , the action to be taken is specified under handler, note that it gets triggered only once.​​ 

 

 

 

If you run the YAML file , it installs the apache in web1 and the mysql in db1, it also updates the values in the httpd.j2 and index.j2 file and deploys the configuration and the index file, Later iptables are stopped in web1 and db1 to allow external connection.​​ 

 

 

When we open the webserver url in the local system , you can see that the file is successfully deployed.​​ 

 

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/

 

 

 

demo6.tar

Related posts

Leave a Comment