{"id":7335,"date":"2018-04-06T09:11:45","date_gmt":"2018-04-06T01:11:45","guid":{"rendered":"http:\/\/rmohan.com\/?p=7335"},"modified":"2018-04-06T09:11:45","modified_gmt":"2018-04-06T01:11:45","slug":"jinja2-templates-in-ansible","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7335","title":{"rendered":"JINJA2 templates in ansible"},"content":{"rendered":"<p><strong>JINJA2 templates in ansible<\/strong><\/p>\n<p>[root@workstation ~]# ansible -m ping all<br \/>\n192.168.1.23 | SUCCESS =&gt; {<br \/>\n\u201cchanged\u201d: false,<br \/>\n\u201cping\u201d: \u201cpong\u201d<br \/>\n}<br \/>\n192.168.1.22 | SUCCESS =&gt; {<br \/>\n\u201cchanged\u201d: false,<br \/>\n\u201cping\u201d: \u201cpong\u201d<br \/>\n}<\/p>\n<p>=========================================<br \/>\n[root@workstation ~]# vim motd.j2<br \/>\nthis is {{ ansible_hostname }}.<br \/>\ntoday\u2019s date is {{ ansible_date_time.date }}<br \/>\nyou can ask {{ system_owner }} for access<br \/>\n==========================================<br \/>\n[root@workstation ~]# vim motd.yaml<br \/>\n\u2014<br \/>\n\u2013 hosts: all<br \/>\nvars:<br \/>\nsystem_owner: root<br \/>\ntasks:<br \/>\n\u2013 template:<br \/>\nsrc: motd.j2<br \/>\ndest: \/etc\/motd<br \/>\nowner: root<br \/>\ngroup: root<br \/>\nmode: 777<br \/>\n========================================<br \/>\n[root@workstation ~]# cat \/etc\/motd<br \/>\n========================================<br \/>\n[root@workstation ~]# ansible-playbook \u2013syntax-check motd.yaml<\/p>\n<p>playbook: motd.yaml<\/p>\n<p>[root@workstation ~]# ansible-playbook -C motd.yaml<\/p>\n<p>PLAY [all] *********************************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [192.168.1.22]<br \/>\nok: [192.168.1.23]<\/p>\n<p>TASK [template] ****************************************************************<br \/>\nchanged: [192.168.1.22]<br \/>\nchanged: [192.168.1.23]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\n192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0<br \/>\n192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0<br \/>\n=======================================<br \/>\n[root@workstation ~]# ansible-playbook motd.yaml -vv<br \/>\nUsing \/etc\/ansible\/ansible.cfg as config file<\/p>\n<p>PLAYBOOK: motd.yaml ************************************************************<br \/>\n1 plays in motd.yaml<\/p>\n<p>PLAY [all] *********************************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [192.168.1.22]<br \/>\nok: [192.168.1.23]<\/p>\n<p>TASK [template] ****************************************************************<br \/>\ntask path: \/root\/motd.yaml:6<br \/>\nchanged: [192.168.1.22] =&gt; {\u201cchanged\u201d: true, \u201cchecksum\u201d: \u201c86167eb96fcd968f9ce2403d02fd99de5454cffa\u201d, \u201cdest\u201d: \u201c\/etc\/motd\u201d, \u201cgid\u201d: 0, \u201cgroup\u201d: \u201croot\u201d, \u201cmd5sum\u201d: \u201c35128946a8173580f8c13e86523be1a1\u201d, \u201cmode\u201d: \u201c01411\u201d, \u201cowner\u201d: \u201croot\u201d, \u201csecontext\u201d: \u201csystem_u:object_r:etc_t:s0\u201d, \u201csize\u201d: 84, \u201csrc\u201d: \u201c\/root\/.ansible\/tmp\/ansible-tmp-1485712899.32-75249962942078\/source\u201d, \u201cstate\u201d: \u201cfile\u201d, \u201cuid\u201d: 0}<br \/>\nchanged: [192.168.1.23] =&gt; {\u201cchanged\u201d: true, \u201cchecksum\u201d: \u201c5619e003a6b2617b359b8a369c3f404035ab0b18\u201d, \u201cdest\u201d: \u201c\/etc\/motd\u201d, \u201cgid\u201d: 0, \u201cgroup\u201d: \u201croot\u201d, \u201cmd5sum\u201d: \u201c4429a262ca0c9cca26b2bc66482abfbc\u201d, \u201cmode\u201d: \u201c01411\u201d, \u201cowner\u201d: \u201croot\u201d, \u201csecontext\u201d: \u201csystem_u:object_r:etc_t:s0\u201d, \u201csize\u201d: 80, \u201csrc\u201d: \u201c\/root\/.ansible\/tmp\/ansible-tmp-1485712899.32-157949107190502\/source\u201d, \u201cstate\u201d: \u201cfile\u201d, \u201cuid\u201d: 0}<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\n192.168.1.22 : ok=2 changed=1 unreachable=0 failed=0<br \/>\n192.168.1.23 : ok=2 changed=1 unreachable=0 failed=0<br \/>\n==============================================<br \/>\n[root@servera ~]# logout<br \/>\nConnection to 192.168.1.23 closed.<br \/>\nroot@rmohan:~$ ssh root@192.168.1.23<br \/>\nroot@192.168.1.23\u2019s password:<br \/>\nLast login: Sun Jan 29 23:29:06 2017 from workstation<br \/>\nthis is servera.<br \/>\ntoday\u2019s date is 2017-01-29<br \/>\nyou can ask root for access<br \/>\n==============================================<br \/>\nroot@rmohan:~$ ssh root@192.168.1.22<br \/>\nroot@192.168.1.22\u2019s password:<br \/>\nLast login: Sun Jan 29 23:31:40 2017 from workstation.example.com<br \/>\nthis is workstation.<br \/>\ntoday\u2019s date is 2017-01-29<br \/>\nyou can ask root for access<br \/>\n================================================<\/p>\n<p>[root@workstation ~]# vim inventory<br \/>\n[web]<br \/>\nnode1.rmohan.com<br \/>\nnode2.rmohan.com<\/p>\n<p>[root@workstation ~]# rm -rf \/etc\/motd<\/p>\n<p>[root@servera ~]# rm -rf \/etc\/motd<\/p>\n<p>[root@workstation ~]# ansible-playbook -i inventory \u2013limit node1.rmohan.com motd.yaml<\/p>\n<p>PLAY [all] *********************************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [node1.rmohan.com]<\/p>\n<p>TASK [template] ****************************************************************<br \/>\nchanged: [node1.rmohan.com]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\nnode1.rmohan.com : ok=2 changed=1 unreachable=0 failed=0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JINJA2 templates in ansible<\/p>\n<p>[root@workstation ~]# ansible -m ping all 192.168.1.23 | SUCCESS =&gt; { \u201cchanged\u201d: false, \u201cping\u201d: \u201cpong\u201d } 192.168.1.22 | SUCCESS =&gt; { \u201cchanged\u201d: false, \u201cping\u201d: \u201cpong\u201d }<\/p>\n<p>========================================= [root@workstation ~]# vim motd.j2 this is {{ ansible_hostname }}. today\u2019s date is {{ ansible_date_time.date }} you can ask {{ system_owner }} for access ========================================== [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7335"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7335"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7335\/revisions"}],"predecessor-version":[{"id":7336,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7335\/revisions\/7336"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}