{"id":1737,"date":"2020-03-22T16:49:25","date_gmt":"2020-03-22T13:49:25","guid":{"rendered":"https:\/\/artem.services\/?p=1733"},"modified":"2020-03-22T16:56:49","modified_gmt":"2020-03-22T13:56:49","slug":"1737","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1737&lang=en","title":{"rendered":"Ansible &#8212; Get the IP address of another host from the hosts file"},"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>An example of how to get the <strong>IP<\/strong> address of another host from the &quot;<strong>hosts<\/strong>&quot; file by performing a task on another host.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>server1<\/strong> &#8212; <span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">our <strong>Playbook<\/strong> will be executed on it<\/span><\/span><\/li>\n<li><strong>server2<\/strong> &#8212; <span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">we need its <strong>IP<\/strong> address in the <strong>Playbook<\/strong><\/span><\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>hosts:<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n[default]\r\nserver1 ansible_host=192.168.1.101\r\nserver2 ansible_host=192.168.1.102\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In order to get the IP address, we will use &quot;<strong>hostvars<\/strong>&quot;, where we indicate the name of the server we need, in our case it is &quot;<strong>server2<\/strong>&quot;<\/p>\n<h3>Playbook:<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n---\r\n \r\n- name: example\r\n  gather_facts: No\r\n  hosts: server1\r\n  \r\n  tasks:\r\n    - name: get-ip-from-server2\r\n      debug:\r\n        msg: &quot;Server2 IP address is {{ hostvars[ 'server2' ].ansible_default_ipv4.address }}&quot;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Output:<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nok: [server1] =&gt; {\r\n    &quot;msg&quot;: &quot;Server2 IP address is 192.168.1.102&quot;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An example of how to get the IP address of another host from the &quot;hosts&quot; file by performing a task on another host. &nbsp; server1 &#8212; our Playbook will be executed on it server2 &#8212; we need its IP address in the Playbook &nbsp; hosts: &nbsp; In order to get the IP address, we will &hellip; <a href=\"https:\/\/artem.services\/?p=1737&#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; Get the IP address of another host from the hosts file&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,1389,1371,1379,1391],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1737"}],"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=1737"}],"version-history":[{"count":3,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1737\/revisions"}],"predecessor-version":[{"id":1740,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1737\/revisions\/1740"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}