{"id":1033,"date":"2024-02-23T09:10:06","date_gmt":"2024-02-23T09:10:06","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1033"},"modified":"2024-02-23T16:42:28","modified_gmt":"2024-02-23T16:42:28","slug":"ansible-playbook-example-and-execution","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1033","title":{"rendered":"Ansible &#8211; Playbook example and execution"},"content":{"rendered":"<p>In this example we will write a simple playbook in Ansible, that will create a new user in a managed host.<\/p>\n<p>For this we need to ensure the managed host is present in the &#8220;inventory&#8221; file that will be used<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat inventory<\/p>\n<p>===== ====<\/p>\n<p>192.168.132.129<br \/>\n192.168.132.130<\/p>\n<p>192.168.132.135<br \/>\nservera.example.com<\/p>\n<p>[webservers]<br \/>\nservera.example.com<br \/>\nserverb.example.com<\/p>\n<p>[dbservers]<br \/>\nserverc.example.com<br \/>\nserverb.example.com<br \/>\nservera.example.com<\/p>\n<p>[myGroups:children]<br \/>\nwebservers<br \/>\ndbservers<\/p>\n<p>[testGRP]<br \/>\n192.168.132.135<\/p>\n<p>[testGRP:vars]<br \/>\ntheUser = shiju500<\/p>\n<p>===== ====<\/p>\n<p>Ensure the Ansible server can SSH to the managed node without a password<\/p>\n<p>Let us create a new playbook named\u00a0shijuplaybook.yml<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat shijuplaybook.yml<\/p>\n<p>========= ========<br \/>\n&#8211; &#8211; &#8211;<br \/>\n&#8211; name: shiju sample playbook<br \/>\n&nbsp; &nbsp; hosts: 192.168.132.130<br \/>\n&nbsp; &nbsp; tasks:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Add a user shiju100<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.user:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: shiju100<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: present<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Installing httpd package<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.dnf:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: httpd<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: present<br \/>\n========= ========<\/p>\n<p>Now let us check the syntax of the newly created yml file before actually executing the playbook<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout shijuplaybook.yml &#8211;syntax-check<br \/>\nplaybook: \/root\/shijuplaybook.yml<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout shijuplaybook.yml<\/p>\n<p>==== ===<\/p>\n<p>PLAY [shiju sample playbook] ***************************************************<\/p>\n<p>TASK [Gathering Facts] *********************************************************<br \/>\nok: [192.168.132.130]<\/p>\n<p>TASK [Add a user shiju100] *****************************************************<br \/>\nchanged: [192.168.132.130]<\/p>\n<p>TASK [Install httpd server] ****************************************************<br \/>\nchanged: [192.168.132.130]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\n192.168.132.130 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<br \/>\n[root@centos9vm ~]#<\/p>\n<p>==== ===<\/p>\n<p>Verify is the new user got added in the node 192.168.132.130<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this example we will write a simple playbook in Ansible, that will create a new user in a managed host. For this we need <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1033\" title=\"Ansible &#8211; Playbook example and execution\">[&#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-1033","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\/1033","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=1033"}],"version-history":[{"count":16,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1092,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions\/1092"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}