{"id":1237,"date":"2024-04-17T08:21:18","date_gmt":"2024-04-17T08:21:18","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1237"},"modified":"2024-04-17T08:30:31","modified_gmt":"2024-04-17T08:30:31","slug":"ansible-including-tasks-files-and-importing-playbooks","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1237","title":{"rendered":"Ansible: Including tasks files and importing playbooks"},"content":{"rendered":"<p>The include_tasks module imports a list of tasks to the current playbook for execution. It is one way for organizing tasks in separate files and it also helps in reusing the tasks in other playbooks.<\/p>\n<p>The import_playbook imports the whole playbook that includes tasks, variables, etc from another file to the calling playbook.<\/p>\n<p>The below is an example where tasks and playbooks are included\/imported from separate files.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat environment.yml<\/p>\n<p>==== ====<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em> &#8211; name: Install the {{ package }} package<\/em><br \/>\n<em> &nbsp; &nbsp; ansible.builtin.dnf:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; name: &#8220;{{ package }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; state: latest<\/em><\/p>\n<p><em>&#8211; name: Start the {{ service }} service<\/em><br \/>\n<em>  &nbsp; &nbsp; ansible.builtin.service:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; name: &#8220;{{ service }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; state: started<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; enabled: true<\/em><\/p>\n<p>==== ====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat test.yml<\/p>\n<p>==== ===<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Test web service<\/em><br \/>\n<em>  &nbsp; &nbsp; hosts: testGRP<\/em><br \/>\n<em>  &nbsp; &nbsp; become: false<\/em><br \/>\n<em>  &nbsp; &nbsp; tasks:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; name: connect to internet web server<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; ansible.builtin.uri:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; url: &#8220;{{ url }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; status_code: 200<\/em><\/p>\n<p>==== ===<\/p>\n<p>[root@centos9vm ~]# cat firewall.yml<\/p>\n<p>===== ===<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em> &#8211; name: Install the firewall<\/em><br \/>\n<em>  &nbsp; &nbsp; ansible.builtin.dnf:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; name: &#8220;{{ firewall_pkg }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; state: latest<\/em><\/p>\n<p><em>&#8211; name: Start the firewall<\/em><br \/>\n<em>  &nbsp; &nbsp; ansible.builtin.service:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; name: &#8220;{{ firewall_svc }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; state: started<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; enabled: true<\/em><\/p>\n<p><em>&#8211; name: Open port for {{ rule }}<\/em><br \/>\n<em>  &nbsp; &nbsp; ansible.posix.firewalld:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; service: &#8220;{{ item }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; state: enabled<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; permanent: true<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; immediate: true<\/em><br \/>\n<em>  &nbsp; &nbsp; loop: &#8220;{{ rule }}&#8221;<\/em><\/p>\n<p>======<\/p>\n<p>Now let us put his into a main playbook, to manage the node 192.168.0.129.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat playbook.yml<\/p>\n<p>===== ===<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em> &#8211; name: Configure web server<\/em><br \/>\n<em>  &nbsp; &nbsp; hosts: 192.168.0.129<\/em><br \/>\n<em>  &nbsp; &nbsp; tasks:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; name: Include the environment task file and set the variable<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; include_tasks: environment.yml<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; vars:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; package: httpd<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; service: httpd<\/em><\/p>\n<p><em>    &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Include the firewall task file and set the variables<\/em><br \/>\n<em>    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; include_tasks: firewall.yml<\/em><br \/>\n<em>    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vars:<\/em><br \/>\n<em>    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; firewall_pkg: firewalld<\/em><br \/>\n<em>    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; firewall_svc: firewalld<\/em><br \/>\n<em>    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; rule:<\/em><br \/>\n<em>    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; http<\/em><br \/>\n<em>    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; https<\/em><\/p>\n<p><em>&#8211; name: Import test play and set the variable<\/em><br \/>\n<em>  &nbsp; &nbsp; import_playbook: test.yml<\/em><br \/>\n<em>  &nbsp; &nbsp; vars:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; url: &#8216;http:\/\/192.168.0.129&#8217;<\/em><\/p>\n<p>===== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout playbook.yml<\/p>\n<p>==== ===<\/p>\n<p><em>PLAY [Configure web server] ****************************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>TASK [Include the environment task file and set the variable] ******************<\/em><br \/>\n<em>included: \/root\/environment.yml for 192.168.0.129<\/em><\/p>\n<p><em>TASK [Install the httpd package] ***********************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>TASK [Start the httpd service] *************************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>TASK [Include the firewall task file and set the variables] ********************<\/em><br \/>\n<em>included: \/root\/firewall.yml for 192.168.0.129<\/em><\/p>\n<p><em>TASK [Install the firewall] ****************************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>TASK [Start the firewall] ******************************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>TASK [Open port for [&#8216;http&#8217;, &#8216;https&#8217;]] *****************************************<\/em><br \/>\n<em>ok: [192.168.0.129] =&gt; (item=http)<\/em><br \/>\n<em>ok: [192.168.0.129] =&gt; (item=https)<\/em><\/p>\n<p><em>TASK [Include the placeholder task file and set the variable] ******************<\/em><br \/>\n<em>included: \/root\/placeholder.yml for 192.168.0.129<\/em><\/p>\n<p><em>TASK [Create placeholder file] *************************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>PLAY [Test web service] ********************************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>TASK [connect to internet web server] ******************************************<\/em><br \/>\n<em>ok: [192.168.0.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.0.129 : ok=12 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>=== ====<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The include_tasks module imports a list of tasks to the current playbook for execution. It is one way for organizing tasks in separate files and <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1237\" title=\"Ansible: Including tasks files and importing playbooks\">[&#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-1237","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\/1237","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=1237"}],"version-history":[{"count":15,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1237\/revisions"}],"predecessor-version":[{"id":1252,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1237\/revisions\/1252"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1237"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}