{"id":1141,"date":"2024-03-11T07:07:07","date_gmt":"2024-03-11T07:07:07","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1141"},"modified":"2024-03-12T05:30:12","modified_gmt":"2024-03-12T05:30:12","slug":"ansible-playbook-running-task-based-on-condition","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1141","title":{"rendered":"Ansible: Playbook running task based on condition"},"content":{"rendered":"<p>At times one may want to run certain tasks or command only if certain conditions are met.<\/p>\n<p>For example add a user called &#8220;RedhatUser01&#8221; only if the managed node is a Redhat node. Similarly add a user named &#8220;centosUser01&#8221; only on CentOS managed nodes.<\/p>\n<p>Below is a playbook that uses conditions to achieve the above mentioned example.<\/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<\/em><br \/>\n&nbsp; &nbsp; hosts: 192.168.48.129<\/em><br \/>\n&nbsp; &nbsp; vars:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  theRHUser:<\/em><br \/>\n<em><em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  RedhatUser01<\/em><br \/>\n<em><em>&nbsp; &nbsp; &nbsp; &nbsp;  theCOSuser:<\/em><br \/>\n<em><em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  centosUser01<\/em><\/p>\n<p>&nbsp; &nbsp; tasks:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &#8211; name: Add a user in Redhat<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.user:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  name: &#8220;{{ theRHUser }}&#8221;<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  state:\u00a0present<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  when: ansible_facts[&#8216;distribution&#8217;] == &#8220;Redhat&#8221;<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp;  &#8211; name: Add a user in CentOS node<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.user:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  name: &#8220;{{ theCOSuser }}&#8221;<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  state:\u00a0present<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  when: ansible_facts[&#8216;distribution&#8217;] == &#8220;CentOS&#8221;<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp;  &#8211; name: Check distribution name<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  ansible.builtin.debug:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  var: ansible_facts[&#8216;distribution&#8217;]<\/em><\/p>\n<p>=== ====<\/p>\n<p>Now let us run the playbook<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout shijuplaybook.yml<\/p>\n<p>===== ==<\/p>\n<p><em>PLAY [shiju sample playbook] ***************************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Add a user in Redhat] ****************************************************<\/em><br \/>\n<em><strong>skipping:<\/strong> [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Add a user in CentOS node] ***********************************************<\/em><br \/>\n<em><strong>ok:<\/strong> [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Check distribution name] *************************************************<\/em><br \/>\n<em>ok: [192.168.48.129] =&gt; {<\/em><br \/>\n<em> &#8220;ansible_facts[&#8216;distribution&#8217;]&#8221;: &#8220;CentOS&#8221;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=<\/em>0<\/p>\n<p>===== =====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;tail -n 3 \/etc\/passwd&#8217;<\/p>\n<p>=======<br \/>\n<em>apache:x:48:48:Apache:\/usr\/share\/httpd:\/sbin\/nologin<\/em><br \/>\n<em>user800:x:1010:1010::\/home\/user800:\/bin\/bash<\/em><br \/>\n<em><strong>centosUser01<\/strong>:x:1011:1011::\/home\/centosUser01:\/bin\/bash<\/em><\/p>\n<p>========<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>At times one may want to run certain tasks or command only if certain conditions are met. For example add a user called &#8220;RedhatUser01&#8221; only <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1141\" title=\"Ansible: Playbook running task based on condition\">[&#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-1141","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\/1141","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=1141"}],"version-history":[{"count":13,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions"}],"predecessor-version":[{"id":1157,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions\/1157"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1141"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}