HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Creating a cmd task with all the private ip address details collected from gather_facts

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
theaddresscollectedgather_factsallcreatingwithprivatedetailsfrom

Problem

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 :
      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?

Solution

Q: "Run a set_facts command which will get private ip from each host."

A: You can either run a command, for example,
- shell: 'ifconfig em0 | grep inet | cut -w -f3'
register: out


and use the registered variable
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
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,

servers_list: "{{ cmnd_ip.values()|
product(['8089'])|
map('join', ':')|
join(',') }}"


gives
servers_list: 10.1.0.51:8089,10.1.0.52:8089,10.1.0.53:8089


Example of a complete playbook for testing
- 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
shell> cat hosts
[test]
test_01
test_02
test_03


gives (abrdiged)
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

Context

StackExchange DevOps Q#18576, answer score: 2

Revisions (0)

No revisions yet.