0

I have a template file config.j2.

{% for host in groups['dbs'] %}
ips= {{ hostvars[host].ansible_default_ipv4.address }}
{% endfor %}

My output is:

   ips= 192.168.231.91
   ips= 192.168.231.92
   ips= 192.168.231.93

I want save the output in an array variable like this:

ips=192.168.231.91,192.168.231.92,192.168.231.93

How can do this? thanks.

Edit and output after finding a solution:

  - set_fact:
  ips: []  
  run_once: true

- set_fact:
  ips: "{{ips}} + ['{{hostvars[item].ansible_default_ipv4.address}}']"  
  with_inventory_hostnames:
  - dbs  
  run_once: true

- name: Save ip servers
  template: src=conf.j2 dest=/root


TASK [Gathering Facts] *************************************************************************************************************************
ok: [db1]
ok: [db2]
ok: [db3]

TASK [get-var : set_fact] **********************************************************************************************************************
ok: [db1]

TASK [get-var : set_fact] **********************************************************************************************************************
ok: [db1] => (item=db1)
ok: [db1] => (item=db3)
ok: [db1] => (item=db2)

TASK [get-var : Save ip servers] ***************************************************************************************************************
ok: [db1]
ok: [db2]
ok: [db3]

PLAY RECAP *************************************************************************************************************************************
db1                        : ok=4    changed=0    unreachable=0    failed=0   
db2                        : ok=2    changed=0    unreachable=0    failed=0   
db3                        : ok=2    changed=0    unreachable=0    failed=0   

Finally output in template on hosts (dbs):

[root@db1 ~]# cat conf.j2 
ips=[]
[root@db1 ~]# 

3 Answers 3

1

One solution in a playbook would be to initialize an empty list, then append to it looping over hostnames in your inventory group:

- set_fact:
    ips: []
  run_once: true

- set_fact:
    ips: "{{ips}} + ['{{hostvars[item].ansible_default_ipv4.address}}']"
  with_inventory_hostnames:
    - dbs
  run_once: true

- template:
    src: config.j2
    dest: /tmp/whatever

with a template file config.j2 containing

ips={{ ips|to_yaml }}
1
  • thanks for answered. but array is empty. please look edit question.
    – pyramid13
    Jan 27, 2018 at 15:19
0

Here is how I got it working

  - set_fact:
      ips="[]"
    run_once: true

  - set_fact:
      ips="{{ ips }} + ['{{ hostvars[item].ansible_default_ipv4.address }}']"  
    with_inventory_hostnames:
      - dbs
    run_once: true

  - name: get ip list locally
    local_action: copy content="{{ ips }}" dest=files/nodes
    run_once: true

files/nodes

["192.168.1.13", "192.168.1.8", "192.168.1.14", "192.168.1.6", "192.168.1.9"]
1
  • If you want to bypass writing to a file, the array is there for a template to use {% for ip in ips %}"{{ ip }}"{% endfor %} Apr 11, 2018 at 13:25
0

if you use variable ansible_host for each host in your inventory You can do it like this:

- set_fact: nodelist={%for host in groups['dbs']%}{{hostvars[host].ansible_host}}{% 
if not loop.last %},{% endif %}{% endfor %}

OR by your conditions:

- set_fact: nodelist={%for host in groups['dbs']%} 
{{ hostvars[host].ansible_default_ipv4.address }}{% if not loop.last %},{% endif %}{% 
endfor %}

Note: code should be in one line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .