Answers: <p>Q: <em><strong>&quot;Run a set_facts command which will get private ip from each host.&quot;</strong></em></p>
  <p>A: You can either run a command, for example,</p>
  <pre class="lang-yaml prettyprint-override"><code>  - shell: 'ifconfig em0 | grep inet | cut -w -f3'
      register: out
  </code></pre>
  <p>and use the registered variable</p>
  <pre class="lang-yaml prettyprint-override"><code>  cmnd_ip: &quot;{{ dict(ansible_play_hosts|
                      zip(ansible_play_hosts|
                          map('extract', hostvars, ['out', 'stdout']))) }}&quot;
  </code></pre>
  <p>, or you can run the module <em>setup</em> and use the facts, for example</p>
  <pre class="lang-yaml prettyprint-override"><code>  fact_ip: &quot;{{ dict(ansible_play_hosts|
                      zip(ansible_play_hosts|
                          map('extract', hostvars, ['ansible_em0', 'ipv4']))) }}&quot;
  </code></pre>
  <p>Notes:</p>
  <ul>
  <li><p>Fit the command to your needs.</p>
  </li>
  <li><p>When you use <em>setup</em> the format of facts may vary among the operating systems.</p>
  </li>
  <li><p>There might be more addresses assigned to an interface. In this case, you'll have to decide which one to use.</p>
  </li>
  <li><p>Create the list of the servers. For example,</p>
  </li>
  </ul>
  <pre class="lang-yaml prettyprint-override"><code>  servers_list: &quot;{{ cmnd_ip.values()|
                      product(['8089'])|
                      map('join', ':')|
                      join(',') }}&quot;
  </code></pre>
  <p>gives</p>
  <pre class="lang-yaml prettyprint-override"><code>  servers_list: 10.1.0.51:8089,10.1.0.52:8089,10.1.0.53:8089
  </code></pre>
  <hr>
  <sup>
  <p>Example of a complete playbook for testing</p>
  <pre class="lang-yaml prettyprint-override"><code>- hosts: all
  
    vars:
  
      fact_ip: &quot;{{ dict(ansible_play_hosts|
                        zip(ansible_play_hosts|
                            map('extract', hostvars, ['ansible_em0', 'ipv4']))) }}&quot;
  
      cmnd_ip: &quot;{{ dict(ansible_play_hosts|
                        zip(ansible_play_hosts|
                            map('extract', hostvars, ['out', 'stdout']))) }}&quot;
  
    tasks:
  
      - name: Get facts
        block:
  
          - setup:
          - debug:
              var: fact_ip
            run_once: true
  
        tags: fact
  
      - name: Run command
        block:
  
          - shell: 'ifconfig em0 | grep inet | cut -w -f3'
            register: out
          - debug:
              var: cmnd_ip
            run_once: true
  
        tags: cmnd
  </code></pre>
  <p>given the inventory</p>
  <pre class="lang-ini prettyprint-override"><code>shell&gt; cat hosts
  [test]
  test_01
  test_02
  test_03
  </code></pre>
  <p>gives (abrdiged)</p>
  <pre class="lang-yaml prettyprint-override"><code>PLAY [all] ***********************************************************************************
  
  TASK [setup] *********************************************************************************
  ok: [test_01]
  ok: [test_03]
  ok: [test_02]
  
  TASK [debug] *********************************************************************************
  ok: [test_01] =&gt; 
    fact_ip:
      test_01:
      - address: 10.1.0.51
        broadcast: 10.1.0.51
        netmask: 255.255.255.255
        network: 10.1.0.51
      test_02:
      - address: 10.1.0.52
        broadcast: 10.1.0.52
        netmask: 255.255.255.255
        network: 10.1.0.52
      test_03:
      - address: 10.1.0.53
        broadcast: 10.1.0.53
        netmask: 255.255.255.255
        network: 10.1.0.53
  
  TASK [shell] *********************************************************************************
  changed: [test_01]
  changed: [test_02]
  changed: [test_03]
  
  TASK [debug] *********************************************************************************
  ok: [test_01] =&gt; 
    cmnd_ip:
      test_01: 10.1.0.51
      test_02: 10.1.0.52
      test_03: 10.1.0.53
  </code></pre>
  </sup>
  
Answers: CC BY-SA 4.0
Answers: 2023-11-21T15:31:51.373Z
Answers: 2023-11-21T17:54:11.750Z
Answers: 2023-11-21T17:54:11.750Z
Answers: Vladimir Botka
Body: <p>I am running an Ansible playbook from my local laptop to AWS connecting on the public IP address. The infra is being spun up by terraform so I don't know the private IP address before hand.  I have around 8 hosts. I want to create a command which needs to have all the private IPs of all 8 hosts.</p>
  <p>I have tried <code>gather_facts</code> method but I don’t know how to get values from all 8 hosts assigned to some variable, so that I can use it at another task.</p>
  <pre><code>    - name: initialise cluster
        command:
          cmd: /opt/splunk/bin/splunk splunk init shcluster-config -auth admin:changed -mgmt_uri https://{{ private ip address of host1 }}:8089 -replication_port 34567 -replication_factor 2 -conf_deploy_fetch_url https://{private ip address of host2}:8089 -secret mykey -shcluster_label shcluster1
        become: true
        become_user: root
        ignore_errors: yes
        async: 10
        poll: 0
  
  - name: set cluster 
        command:
          cmd: /opt/splunk/bin/splunk splunk bootstrap shcluster-captain -servers_list &quot;{{private ip of 1st host}}:8089,{{ private ip of 2nd host }}:8089,{{ private ip of 3rd host }}:8089,etc...&quot; -auth &lt;username&gt;:&lt;password&gt;
        become: true
        become_user: root
        ignore_errors: yes
        async: 10
        poll: 0
  </code></pre>
  <p>How do I run a <code>set_facts</code> command which will get the private ip from each hosts and assign it to a cmd task?</p>
  
Tags: <ansible><ansible-inventory>
Title: Creating a cmd task with all the private ip address details collected from gather_facts
UserDisplayName: ranjit abraham
ThreadText: # Creating a cmd task with all the private ip address details collected from gather_facts
  
  (Asked by: username_0 on 2023-11-21)
  
  I am running an Ansible playbook from my local laptop to AWS connecting on the public IP address. The infra is being spun up by terraform so I don't know the private IP address before hand. I have around 8 hosts. I want to create a command which needs to have all the private IPs of all 8 hosts.
  
  I have tried `gather_facts` method but I don’t know how to get values from all 8 hosts assigned to some variable, so that I can use it at another task.
  
  ```
      - name: initialise cluster
        command:
          cmd: /opt/splunk/bin/splunk splunk init shcluster-config -auth admin:changed -mgmt_uri https://{{ private ip address of host1 }}:8089 -replication_port 34567 -replication_factor 2 -conf_deploy_fetch_url https://{private ip address of host2}:8089 -secret mykey -shcluster_label shcluster1
        become: true
        become_user: root
        ignore_errors: yes
        async: 10
        poll: 0
  
  - name: set cluster 
        command:
          cmd: /opt/splunk/bin/splunk splunk bootstrap shcluster-captain -servers_list "{{private ip of 1st host}}:8089,{{ private ip of 2nd host }}:8089,{{ private ip of 3rd host }}:8089,etc..." -auth <username>:<password>
        become: true
        become_user: root
        ignore_errors: yes
        async: 10
        poll: 0
  
  ```
  
  How do I run a `set_facts` command which will get the private ip from each hosts and assign it to a cmd task?
  
  **Tags:** ansible, ansible-inventory
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-11-21. Score: 2 | Accepted answer
  
  Q: ***"Run a set\_facts command which will get private ip from each host."***
  
  A: You can either run a command, for example,
  
  ```yaml
    - shell: 'ifconfig em0 | grep inet | cut -w -f3'
      register: out
  
  ```
  
  and use the registered variable
  
  ```yaml
    cmnd_ip: "{{ dict(ansible_play_hosts|
                      zip(ansible_play_hosts|
                          map('extract', hostvars, ['out', 'stdout']))) }}"
  
  ```
  
  , or you can run the module *setup* and use the facts, for example
  
  ```yaml
    fact_ip: "{{ dict(ansible_play_hosts|
                      zip(ansible_play_hosts|
                          map('extract', hostvars, ['ansible_em0', 'ipv4']))) }}"
  
  ```
  
  Notes:
  
  * Fit the command to your needs.
  * When you use *setup* the format of facts may vary among the operating systems.
  * There might be more addresses assigned to an interface. In this case, you'll have to decide which one to use.
  * Create the list of the servers. For example,
  
  ```yaml
    servers_list: "{{ cmnd_ip.values()|
                      product(['8089'])|
                      map('join', ':')|
                      join(',') }}"
  
  ```
  
  gives
  
  ```yaml
    servers_list: 10.1.0.51:8089,10.1.0.52:8089,10.1.0.53:8089
  
  ```
  
  ---
  
  Example of a complete playbook for testing
  
  ```yaml
  - hosts: all
  
    vars:
  
      fact_ip: "{{ dict(ansible_play_hosts|
                        zip(ansible_play_hosts|
                            map('extract', hostvars, ['ansible_em0', 'ipv4']))) }}"
  
      cmnd_ip: "{{ dict(ansible_play_hosts|
                        zip(ansible_play_hosts|
                            map('extract', hostvars, ['out', 'stdout']))) }}"
  
    tasks:
  
      - name: Get facts
        block:
  
          - setup:
          - debug:
              var: fact_ip
            run_once: true
  
        tags: fact
  
      - name: Run command
        block:
  
          - shell: 'ifconfig em0 | grep inet | cut -w -f3'
            register: out
          - debug:
              var: cmnd_ip
            run_once: true
  
        tags: cmnd
  
  ```
  
  given the inventory
  
  ```ini
  shell> cat hosts
  [test]
  test_01
  test_02
  test_03
  
  ```
  
  gives (abrdiged)
  
  ```yaml
  PLAY [all] ***********************************************************************************
  
  TASK [setup] *********************************************************************************
  ok: [test_01]
  ok: [test_03]
  ok: [test_02]
  
  TASK [debug] *********************************************************************************
  ok: [test_01] => 
    fact_ip:
      test_01:
      - address: 10.1.0.51
        broadcast: 10.1.0.51
        netmask: 255.255.255.255
        network: 10.1.0.51
      test_02:
      - address: 10.1.0.52
        broadcast: 10.1.0.52
        netmask: 255.255.255.255
        network: 10.1.0.52
      test_03:
      - address: 10.1.0.53
        broadcast: 10.1.0.53
        netmask: 255.255.255.255
        network: 10.1.0.53
  
  TASK [shell] *********************************************************************************
  changed: [test_01]
  changed: [test_02]
  changed: [test_03]
  
  TASK [debug] *********************************************************************************
  ok: [test_01] => 
    cmnd_ip:
      test_01: 10.1.0.51
      test_02: 10.1.0.52
      test_03: 10.1.0.53
  
  ```
  
  
  ---
  
  