{"id":1139,"date":"2024-03-11T06:07:56","date_gmt":"2024-03-11T06:07:56","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1139"},"modified":"2024-03-12T05:35:27","modified_gmt":"2024-03-12T05:35:27","slug":"ansible-run-playbook-looping-values-in-variable","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1139","title":{"rendered":"Ansible: Run playbook looping values in variable"},"content":{"rendered":"<p>At times you may want to execute a single tasks with multiple variable values one after the other. For example create 3 user accounts using one task.\u00a0One way is to write three tasks in the same playbook, where each user is created by each task. The other way is to loop the values associated with a variable.<\/p>\n<p>Below is a playbook that created three users using just one task in a playbook<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat adduserLoop.yml<\/p>\n<p>==== ==<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Playbook to create 3 users<\/em><br \/>\n<em> &nbsp; &nbsp; hosts: 192.168.48.129<\/em><br \/>\n<em> &nbsp; &nbsp; vars:<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp; myUser:<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; shiju01<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; shiju02<\/em><br \/>\n<em> &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; shiju03<\/em><br \/>\n<em> &nbsp; &nbsp; tasks:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp;  &#8211; name: Create a user at a time<\/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;{{ item }}&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  state: present<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;  loop: &#8220;{{ myUser }}&#8221;<\/em><\/p>\n<p>===== =====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout adduserLoop.yml<\/p>\n<p>====== ===<\/p>\n<p><em>PLAY [Playbook to create 3 users] **********************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Create a user at a time] *************************************************<\/em><br \/>\n<em>changed: [192.168.48.129] =&gt; (item=shiju01)<\/em><br \/>\n<em>changed: [192.168.48.129] =&gt; (item=shiju02)<\/em><br \/>\n<em>changed: [192.168.48.129] =&gt; (item=shiju03)<\/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==== =====<\/p>\n<p>Let us check if the users got created<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;tail -3 \/etc\/passwd&#8217;<\/p>\n<p>==== ====<br \/>\n<em>shiju01:x:1012:1012::\/home\/shiju01:\/bin\/bash<\/em><br \/>\n<em>shiju02:x:1013:1013::\/home\/shiju02:\/bin\/bash<\/em><br \/>\n<em>shiju03:x:1014:1014::\/home\/shiju03:\/bin\/bash<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>At times you may want to execute a single tasks with multiple variable values one after the other. For example create 3 user accounts using <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1139\" title=\"Ansible: Run playbook looping values in variable\">[&#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-1139","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\/1139","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=1139"}],"version-history":[{"count":10,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1139\/revisions"}],"predecessor-version":[{"id":1163,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1139\/revisions\/1163"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}