{"id":1127,"date":"2024-03-05T04:34:39","date_gmt":"2024-03-05T04:34:39","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1127"},"modified":"2024-03-12T05:45:18","modified_gmt":"2024-03-12T05:45:18","slug":"ansible-modules-and-functions-examples-in-playbooks","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1127","title":{"rendered":"Ansible &#8211; Modules and functions examples in playbooks"},"content":{"rendered":"<p>This document is indented to demonstrate various playbooks that makes use of different modules and their functionalities.<\/p>\n<p style=\"text-align: center;\"><strong><span style=\"text-decoration: underline;\">Pinging managed nodes<\/span><\/strong><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat inventory<\/p>\n<p>====== ======= =<br \/>\n<em>192.168.48.128<\/em><br \/>\n<em>192.168.48.129<\/em><\/p>\n<p><em>[testGRP]<\/em><br \/>\n<em>192.168.48.128<\/em><br \/>\n<em>192.168.48.129<\/em><\/p>\n<p>===== =====<br \/>\n<strong>[root@centos9vm ~]#<\/strong> cat ping_all.yml<\/p>\n<p>========= ====<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: An example to ping all managed nodes in the inventory file<\/em><br \/>\n<em> &nbsp; &nbsp;  hosts: all<\/em><br \/>\n<em> &nbsp; &nbsp;  tasks:<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &#8211; name: Ping all<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.ping:<\/em><\/p>\n<p>==== ==== ==<br \/>\n<strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout ping_all.yml<\/p>\n<p>===== ====<\/p>\n<p><em>PLAY [An example to ping all managed nodes in the inventory file] **************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em> fatal: [192.168.48.128]: UNREACHABLE! =&gt; {&#8220;changed&#8221;: false, &#8220;msg&#8221;: &#8220;Failed to connect to the host via ssh: root@192.168.48.128: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).&#8221;, &#8220;unreachable&#8221;: true}<\/em><br \/>\n<em> ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Ping all] ****************************************************************<\/em><br \/>\n<em> ok: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em> 192.168.48.128 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n<em> 192.168.48.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n<em> Please review the log for errors.<\/em><\/p>\n<p>====== ====<\/p>\n<p>In the above example password-less ssh login was not configured with\u00a0<em>192.168.48.129<\/em><\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>Running Arbitrary Commands on Managed Hosts<\/strong><\/span><\/p>\n<p style=\"text-align: left;\">We can execute scripts and command present in managed nodes from an ansible controller. A use case can be where a script is copied from controller node to managed node, and then executed from the managed node itself.<\/p>\n<p style=\"text-align: left;\">Let us ensure the script of command exists in the managed node:<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;cat \/root\/shiju_touch.sh&#8217;<\/p>\n<p style=\"text-align: left;\">===== ==<br \/>\n<em>#!\/usr\/bin\/bash<\/em><br \/>\n<em>\/usr\/bin\/touch \/tmp\/sam123.txt<\/em><\/p>\n<p style=\"text-align: left;\">===== ===<\/p>\n<p style=\"text-align: left;\">Let us create a playbook<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm ~]#<\/strong> cat runCommand.yml<\/p>\n<p style=\"text-align: left;\">==== ====<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: An example to run a command in managed node<\/em><br \/>\n<em> &nbsp; &nbsp;  hosts: 192.168.48.129<\/em><br \/>\n<em> &nbsp; &nbsp;  tasks:<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &#8211; name: Execute a command<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.command:<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  cmd: \/root\/shiju_touch.sh<\/em><\/p>\n<p style=\"text-align: left;\">===== ====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout runCommand.yml<\/p>\n<p>===== ==<\/p>\n<p><em>PLAY [An example to run a command in 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 [Execute a command] *******************************************************<\/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=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n===== ====<br \/>\n<strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;ls -l \/tmp\/sam*&#8217;<\/p>\n<p>===== ====<br \/>\n<em>-rw-r&#8211;r&#8211;. 1 root root 0 Mar 5 17:37 \/tmp\/sam123.txt<\/em><\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>Check is a\u00a0website can be accessed<\/strong><\/span><\/p>\n<p>Let us create a playbook that installs httpd, created a webpage, and then starts the service. Finally we will verify is the site can be accessed and gets a valid response.<\/p>\n<p>[root@centos9vm ~]# cat acessWebsite.yml<\/p>\n<p>==== ==== ===<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&nbsp; &nbsp;  &#8211; name: install webserver and access webpage<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  hosts: 192.168.48.129<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  tasks:<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &#8211; name: install httpd<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.dnf:<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  name: httpd<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  state: latest<\/em><\/p>\n<p><em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &#8211; name: Add an index file<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.copy:<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  content: &#8220;Hello site\\n&#8221;<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  dest: \/var\/www\/html\/index.html<\/em><\/p>\n<p><em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &#8211; name: start httpd<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.service:<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  name: httpd<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  state: started<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  enabled: true<\/em><\/p>\n<p><em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &#8211; name: verify webpage<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.uri:<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  url: http:\/\/192.168.48.129<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  return_content: true<\/em><br \/>\n<em>&nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  status_code: 200<\/em><\/p>\n<p>==========<\/p>\n<p>Let us run the playbook and verify.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout acessWebsite.yml<\/p>\n<p>===== ===<\/p>\n<p><em>PLAY [install webserver and access webpage] ************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [install httpd] ***********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Add an index file] *******************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [start httpd] *************************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [verify webpage] **********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129<\/em><\/p>\n<p>==== =<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This document is indented to demonstrate various playbooks that makes use of different modules and their functionalities. Pinging managed nodes [root@centos9vm ~]# cat inventory ====== <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1127\" title=\"Ansible &#8211; Modules and functions examples in 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-1127","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\/1127","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=1127"}],"version-history":[{"count":18,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1171,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions\/1171"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}