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 ~]# 
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 }}
  • thanks for answered. but array is empty. please look edit question. – pyramid13 Jan 27 '18 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"]
  • If you want to bypass writing to a file, the array is there for a template to use {% for ip in ips %}"{{ ip }}"{% endfor %} – user2363318 Apr 11 '18 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.