{"id":1730,"date":"2020-03-22T16:14:57","date_gmt":"2020-03-22T13:14:57","guid":{"rendered":"https:\/\/artem.services\/?p=1727"},"modified":"2020-03-22T16:21:01","modified_gmt":"2020-03-22T13:21:01","slug":"1730","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1730&lang=en","title":{"rendered":"Ansible &#8212; Use values in a loop from hosts file on another host"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1393\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo.png\" alt=\"\" width=\"1280\" height=\"720\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo.png 1280w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-300x169.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-768x432.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-1024x576.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-954x537.png 954w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>There is a &quot;<strong>hosts<\/strong>&quot; file in which there is a group of &quot;<strong>db<\/strong>&quot;. These are database instances that play a different role. The goal is to add the role type to the &quot;<strong>hosts<\/strong>&quot; file, so that later we can use this value in a loop that will be executed on a completely different host, for example &quot;<strong>management<\/strong>&quot;<\/p>\n<p>Source &quot;<strong>hosts<\/strong>&quot; file:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n[db]\r\ndb1 ansible_host=192.168.1.101\r\ndb2 ansible_host=192.168.1.102\r\ndb3 ansible_host=192.168.1.103\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Add the &quot;<strong>role<\/strong>&quot; key with the <span class=\"gt-baf-term-text\"><span class=\"gt-baf-cell gt-baf-word-clickable\">necessary<\/span><\/span> value for each host in the &quot;<strong>db<\/strong>&quot; group:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n[db]\r\ndb1 role=development ansible_host=192.168.1.101\r\ndb2 role=staging ansible_host=192.168.1.102\r\ndb3 role=production ansible_host=192.168.1.103\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Playbook<\/strong> example:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n---\r\n\r\n- name: management\r\n  gather_facts: No\r\n  hosts: management\r\n \r\n  tasks:\r\n    - name: debug\r\n      debug:\r\n        msg: &quot;Instance {{ item.1 }} with IP {{ hostvars[ item.1 ].ansible_host }} is {{ hostvars[ item.1 ].role}}&quot;\r\n      with_indexed_items: &quot;{{ groups['db'] }}&quot;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Output<\/strong>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nok: [management] =&gt; (item=[0, u'db1']) =&gt; {\r\n    &quot;msg&quot;: &quot;Instance db1 with IP 192.168.1.101 is development&quot;\r\n}\r\nok: [management] =&gt; (item=[1, u'db2']) =&gt; {\r\n    &quot;msg&quot;: &quot;Instance db2 with IP 192.168.1.102 is staging&quot;\r\n}\r\nok: [management] =&gt; (item=[2, u'db3']) =&gt; {\r\n    &quot;msg&quot;: &quot;Instance db3 with IP 192.168.1.103 is production&quot;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; There is a &quot;hosts&quot; file in which there is a group of &quot;db&quot;. These are database instances that play a different role. The goal is to add the role type to the &quot;hosts&quot; file, so that later we can use this value in a loop that will be executed on a completely different host, &hellip; <a href=\"https:\/\/artem.services\/?p=1730&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;Ansible &#8212; Use values in a loop from hosts file on another host&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1131],"tags":[1133,1371,1379,1381],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1730"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1730"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1730\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1730\/revisions\/1732"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}