{"id":1117,"date":"2024-03-03T13:42:07","date_gmt":"2024-03-03T13:42:07","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1117"},"modified":"2024-07-04T10:20:27","modified_gmt":"2024-07-04T10:20:27","slug":"ansible-templates","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1117","title":{"rendered":"Ansible : Templates &#8211; Jinja2"},"content":{"rendered":"<p>Ansible templates allows users to dynamically generate text-based files using templates, variables, and facts for configuration and other purposes.<\/p>\n<p>The difference between Ansible Facts and Templates is as follows:<\/p>\n<p>An ansible Fact stores variables that can be used by a playbook when executed<\/p>\n<p>An ansible template, which used ansible facts\u00a0and\u00a0pulls values of variable defined in facts, used them to create files which incorporates these values.<\/p>\n<p>Let us create a template file in the controller node in Jinja format:<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat template.j2<\/p>\n<p>===== ====<br \/>\n<em>Welcome to the {{ ansible_facts[&#8216;hostname] }} from the {{ ansible_facts[&#8216;fqdn&#8217;] }}<\/em><\/p>\n<p>{# This line will not appear any where. Below is a variable declared in playbook #}<\/p>\n<p>My name is {{\u00a0 myName }}<\/p>\n<p>===== ===<\/p>\n<p>Let us create a playbook to copy the template to managed node and execute the playbook to create a new file<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat example7.yml<\/p>\n<p>===== ====<br \/>\n<em>\u00a0 \u00a0 &#8211; &#8211; &#8211;<\/em><br \/>\n<em>\u00a0 \u00a0 &#8211; name: Share the jinja tempate with values to managed node<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 hosts: testGRP<\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0 \u00a0 vars:<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0\u00a0myName: SHIJU<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 tasks:<\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: Using copied template to generate file<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ansible.builtin.template:<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 src: \/root\/template.j2<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dest: \/root\/file_created_from_template.txt<\/em><br \/>\n<em>==== ====<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout example7.yml<\/p>\n<p>===== ====<\/p>\n<p><em>PLAY [Share the jinja tempate with values to managed node] *********************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em> ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Copy the template.j2 file to managed node] *******************************<\/em><br \/>\n<em> changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Using copied template to generate file] **********************************<\/em><br \/>\n<em> changed: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em> 192.168.48.129 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>===== ===<\/p>\n<p>Let us verify if the file got created in the managed node<\/p>\n<p><span style=\"color: #800000;\"><strong>[root@centosMYOBvm ~]#<\/strong><\/span> cat file_created_from_template.txt<\/p>\n<p>===== ==== ==<br \/>\n<em>Welcome to the centosMYOBvm from the centosMYOBvm<\/em><\/p>\n<p>SHIJU<br \/>\n===== === ==<\/p>\n<p style=\"text-align: center;\"><strong><span style=\"color: #ff6600;\">Creating a loop using variable list in playbook<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff6600;\"> ====== @@@@@ ======<\/span><\/strong><\/p>\n<p style=\"text-align: left;\">In the below example, we will create a playbook with a variable list called\u00a0<em><strong>fruitList<\/strong><\/em>, and use a jinja template to create a file in managed node that will have names of all fruits in the <em>fruitList<\/em>.<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm ~]#<\/strong> cat temp.yml<\/p>\n<p style=\"text-align: left;\"><em>==== ====<\/em><br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Playbook to install a template<\/em><br \/>\n<em> \u00a0 \u00a0 hosts: dev<\/em><br \/>\n<em> \u00a0 \u00a0 vars:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 myName: Shiju<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 fruitList:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211; Apple<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211; Orange<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211; Grapes<\/em><br \/>\n<em> \u00a0 \u00a0 tasks:<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0&#8211; name: Task to deploy a template<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0ansible.builtin.template:<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0src: shiju.j2<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0dest: \/webdevelop\/index.html<\/em><br \/>\n==== ====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat shiju.j2<br \/>\n===== ==<br \/>\n<em>Welcome to {{ ansible_facts[&#8216;hostname&#8217;] }} on {{ ansible_facts[&#8216;default_ipv4&#8217;][&#8216;address&#8217;] }}<\/em><br \/>\n<em>Welcome to {{ ansible_facts[&#8216;nodename&#8217;] }} on {{ ansible_facts[&#8216;all_ipv4_addresses&#8217;] }}<\/em><\/p>\n<p><em>My name is {{ myName }}<\/em><\/p>\n<p><em>{% for theFruits in fruitList %}<\/em><br \/>\n<em> {{ theFruits }}<\/em><br \/>\n<em>{% endfor %}<\/em><br \/>\n====== ==<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout temp.yml<br \/>\n===== =====<br \/>\nPLAY [Playbook to install a template] ******************************************<\/p>\n<p>TASK [Gathering Facts] *********************************************************<br \/>\nok: [192.168.48.132]<\/p>\n<p>TASK [Task to deploy a template] ***********************************************<br \/>\nok: [192.168.48.132]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\n192.168.48.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<br \/>\n====== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/webdevelop\/index.html&#8217;<br \/>\n===== =====<br \/>\nWelcome to centos9test1 on 192.168.48.132<br \/>\nWelcome to centos9test1 on [&#8216;192.168.48.132&#8217;]<\/p>\n<p>My name is Shiju<\/p>\n<p>Apple<br \/>\nOrange<br \/>\nGrapes<br \/>\n===== ===<\/p>\n<p style=\"text-align: center;\"><span style=\"color: #993300;\"><strong>listing hostnames of group in Inventory file<\/strong><\/span><br \/>\n<span style=\"color: #993300;\"> <strong>===== @@@@@ =======<\/strong><\/span><\/p>\n<p>Now let us write a jinja template to loop through the list of managed hosts in the playbook and create a file in all managed nodes in the list, with the content being hostnames\u00a0 all nodes in the list<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat shiju.j2<\/p>\n<p>======== ====<br \/>\n{% for i in groups[&#8216;dev&#8217;]%}<br \/>\n{{ hostvars[i][&#8216;ansible_facts&#8217;][&#8216;hostname&#8217;] }}<br \/>\n{% endfor %}<\/p>\n<p>====== ==<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat temp.yml<\/p>\n<p><em>====== ===<\/em><br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0\u00a0&#8211; name: Playbook to install a template<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0hosts: dev<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0tasks:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0\u00a0&#8211; name: Task to deploy a template<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0ansible.builtin.template:<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0src: shiju.j2<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0 \u00a0 \u00a0\u00a0 \u00a0\u00a0dest: \/webdevelop\/index.html<\/em><\/p>\n<p><em>===== ===<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat inventory<\/p>\n<p><em>===== ==<\/em><br \/>\n<em>[prod]<\/em><br \/>\n<em>192.168.48.100<\/em><\/p>\n<p><em>[dev]<\/em><br \/>\n<em>192.168.48.132<\/em><br \/>\n<em>192.168.48.129<\/em><br \/>\n<em>===== ==<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout temp.yml<\/p>\n<p>PLAY [Playbook to install a template] *********************************************<\/p>\n<p>TASK [Gathering Facts] ************************************************************<br \/>\nok: [192.168.48.129]<br \/>\nok: [192.168.48.132]<\/p>\n<p>TASK [Task to deploy a template] **************************************************<br \/>\nchanged: [192.168.48.129]<br \/>\nchanged: [192.168.48.132]<\/p>\n<p>PLAY RECAP ************************************************************************<br \/>\n192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<br \/>\n192.168.48.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/p>\n<p>===== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/webdevelop\/index.html&#8217;<br \/>\ncentos9test1<br \/>\ncentosMYOBvm<\/p>\n<p>==== ===<\/p>\n<p>Now let us edit the jinja template to create content for an \/etc\/hosts file<\/p>\n<p>==== ====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat shiju.j2<br \/>\n{% for i in groups[&#8216;dev&#8217;]%}<br \/>\n{{hostvars[i][&#8216;ansible_facts&#8217;][&#8216;default_ipv4&#8217;][&#8216;address&#8217;]}} {{ hostvars[i][&#8216;ansible_facts&#8217;][&#8216;fqdn&#8217;] }} {{ hostvars[i][&#8216;ansible_facts&#8217;][&#8216;hostname&#8217;] }}<br \/>\n{% endfor %}<\/p>\n<p>===== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout temp.yml<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/webdevelop\/index.html&#8217;<\/p>\n<p>==== =====<br \/>\n192.168.48.132\u00a0 \u00a0 \u00a0 \u00a0 centos9test1\u00a0 \u00a0 \u00a0 \u00a0centos9test1<br \/>\n192.168.48.129\u00a0 \u00a0 \u00a0 \u00a0 centosMYOBvm\u00a0 \u00a0 \u00a0 \u00a0centosMYOBvm<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Ansible templates allows users to dynamically generate text-based files using templates, variables, and facts for configuration and other purposes. The difference between Ansible Facts and <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1117\" title=\"Ansible : Templates &#8211; Jinja2\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1117","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1117"}],"version-history":[{"count":23,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1117\/revisions"}],"predecessor-version":[{"id":1570,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1117\/revisions\/1570"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1117"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}