Answers: <p>Q: <em>&quot;<strong>IP address Ansible control machine uses to connect to the client machine.</strong>&quot;</em></p>
  <p>A: Find out to which IP the client <a href="https://docs.ansible.com/ansible/latest/collections/community/general/dig_lookup.html" rel="nofollow noreferrer">resolves</a>, e.g. running at the controller</p>
  <pre class="lang-yaml prettyprint-override"><code>    - debug:
          msg: &quot;{{ lookup('community.general.dig', 'test_21') }}&quot;
  </code></pre>
  <p>gives</p>
  <pre class="lang-yaml prettyprint-override"><code>ok: [localhost] =&gt; 
    msg: 10.1.0.71
  </code></pre>
  <p>If the play is running at the client delegate to localhost, e.g.</p>
  <pre class="lang-yaml prettyprint-override"><code>    - debug:
          msg: &quot;{{ lookup('community.general.dig', inventory_hostname) }}&quot;
        delegate_to: localhost
  </code></pre>
  <p>gives</p>
  <pre class="lang-yaml prettyprint-override"><code>ok: [test_21 -&gt; localhost] =&gt; 
    msg: 10.1.0.71
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-12-07T10:54:19.293Z
Answers: 2021-12-07T13:09:23.677Z
Answers: 2021-12-07T13:09:23.677Z
Answers: Vladimir Botka
Body: <p>I have an Ansible environment in which machines are listed by hostname in the inventories. Occasionally, it is necessary to determine the IP address of a target machine in a playbook (note: in this context, I am only interested in the address of the interface that the Ansible control machine uses to connect to the client machine).</p>
  <p>It seems that this is not easy to do in an OS-independent way: Linux clients define the dictionary <code>default_ipv4</code> in their <code>hostvars</code>, but Windows machines define the array <code>ip_addresses</code>.</p>
  <p>This is how the relevant section of a <code>hostvars</code> dump from a Linux client looks:</p>
  <pre><code>ok: [sampleboxlin] =&gt; {
  &quot;hostvars[inventory_hostname].ansible_facts&quot;: {
      &quot;all_ipv4_addresses&quot;: [
          &quot;123.234.99.99&quot;
      ],
  </code></pre>
  <p>and this is how things look on a Windows client:</p>
  <pre><code>ok: [sampleboxwin] =&gt; {
  &quot;hostvars[inventory_hostname]&quot;: {
    // shortened
    &quot;hostname&quot;: &quot;sampleboxwin&quot;,
          &quot;interfaces&quot;: [
              {
                  &quot;connection_name&quot;: &quot;Ethernet0&quot;,
                  &quot;default_gateway&quot;: &quot;123.234.99.1&quot;,
                  &quot;dns_domain&quot;: null,
                  &quot;interface_index&quot;: 13,
                  &quot;interface_name&quot;: &quot;Intel(R) 82574L Gigabit Network Connection&quot;,
                  &quot;macaddress&quot;: &quot;0b:fu:sc:at:ed:00&quot;
              }
          ],
          &quot;ip_addresses&quot;: [
              &quot;123.234.99.9&quot;
          ],
  </code></pre>
  <p>So, what is the best-practice way of mapping hostnames to the host's IP address [1] in a playbook, in a robust and OS-independent way?</p>
  <hr />
  <p>[1] again, we always assume that the client only has one relevant IP address, and we are interested in the one used to communicate with the Ansible controller</p>
  
Tags: <ansible>
Title: How can I determine an Ansible client's IP address in an OS-independent manner?
UserDisplayName: jstarek
ThreadText: # How can I determine an Ansible client's IP address in an OS-independent manner?
  
  (Asked by: username_0 on 2021-12-07)
  
  I have an Ansible environment in which machines are listed by hostname in the inventories. Occasionally, it is necessary to determine the IP address of a target machine in a playbook (note: in this context, I am only interested in the address of the interface that the Ansible control machine uses to connect to the client machine).
  
  It seems that this is not easy to do in an OS-independent way: Linux clients define the dictionary `default_ipv4` in their `hostvars`, but Windows machines define the array `ip_addresses`.
  
  This is how the relevant section of a `hostvars` dump from a Linux client looks:
  
  ```
  ok: [sampleboxlin] => {
  "hostvars[inventory_hostname].ansible_facts": {
      "all_ipv4_addresses": [
          "123.234.99.99"
      ],
  
  ```
  
  and this is how things look on a Windows client:
  
  ```
  ok: [sampleboxwin] => {
  "hostvars[inventory_hostname]": {
    // shortened
    "hostname": "sampleboxwin",
          "interfaces": [
              {
                  "connection_name": "Ethernet0",
                  "default_gateway": "123.234.99.1",
                  "dns_domain": null,
                  "interface_index": 13,
                  "interface_name": "Intel(R) 82574L Gigabit Network Connection",
                  "macaddress": "0b:fu:sc:at:ed:00"
              }
          ],
          "ip_addresses": [
              "123.234.99.9"
          ],
  
  ```
  
  So, what is the best-practice way of mapping hostnames to the host's IP address [1] in a playbook, in a robust and OS-independent way?
  
  ---
  
  [1] again, we always assume that the client only has one relevant IP address, and we are interested in the one used to communicate with the Ansible controller
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-12-07. Score: 5 | Accepted answer
  
  Q: *"**IP address Ansible control machine uses to connect to the client machine.**"*
  
  A: Find out to which IP the client [resolves](https://docs.ansible.com/ansible/latest/collections/community/general/dig_lookup.html), e.g. running at the controller
  
  ```yaml
      - debug:
          msg: "{{ lookup('community.general.dig', 'test_21') }}"
  
  ```
  
  gives
  
  ```yaml
  ok: [localhost] => 
    msg: 10.1.0.71
  
  ```
  
  If the play is running at the client delegate to localhost, e.g.
  
  ```yaml
      - debug:
          msg: "{{ lookup('community.general.dig', inventory_hostname) }}"
        delegate_to: localhost
  
  ```
  
  gives
  
  ```yaml
  ok: [test_21 -> localhost] => 
    msg: 10.1.0.71
  
  ```
  
  
  ---
  
  