user3511320
user3511320

Reputation: 129

ansible create string from variable comma separted

I have this variable:

amq_ping_hosts:
  - "10.1.1.1"
  - "10.1.1.2"
  - "10.1.1.3"
  - "10.1.1.4"

and I want to create variable with a comma separated string:

ping_hosts: 10.1.1.1,10.1.1.2,10.1.1.3.10.1.1.4

I tried this:

- name: "set fact ping_hosts"
  set_fact:
    ping_hosts: "{{ ping_hosts }} ',' {{ item }}"
  loop: "{{ amq_ping_hosts }}"

but ping_hosts is an undefined variable

Upvotes: 1

Views: 677

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Use join. The tasks below

    - set_fact:
        ping_hosts: "{{ amq_ping_hosts|join(',') }}"
    - debug:
        var: ping_hosts

give

  ping_hosts: 10.1.1.1,10.1.1.2,10.1.1.3,10.1.1.4

Upvotes: 3

ilias-sp
ilias-sp

Reputation: 6685

you can use join function from python:

  tasks:
  - set_fact: 
      ping_hosts: "{{ ','.join(amq_ping_hosts) }}"

  - debug:
      var: ping_hosts

Upvotes: 1

Related Questions