concatenate multiple results

10 views
Skip to first unread message

David Villasmil

unread,
May 25, 2018, 10:32:10 PM5/25/18
to Ansible Project
Hellos guys,

I have a result from ec2_instance_facts, and i can get the ip addresses like:
  - name: Get IP addresses from results
    set_fact
:
      inject_ips
: “{{ facts_output | json_query(‘instances[].public_ip_address’) }}”

and that gives me something like:

ok: [localhost] => {
   
"ansible_facts": {
       
"inject_ips": [
           
"1.2.3.4",
           
"1.2.3.4",
           
"1.2.3.4"
       
]

   
},
   
"changed": false
}


but i need the list to include the name, like:

ok: [localhost] => {
   
"ansible_facts": {
       
"inject_ips": [
           
"1.2.3.4,name1",
            "1.2.3.4,name2",
            "1.2.3.4,name3"
        ]

   
},
   
"changed": false
}


I can also get the name with:

  - name: Get IP addresses from results
    set_fact
:
      inject_ips
: “{{ facts_output | json_query(‘instances[].tags[].Name’) }}”

But i don't know how to concatenate them, i tried:

  - name: Get IP addresses from results
    set_fact
:
      inject_ips
: “{{ facts_output | json_query(‘instances[].public_ip_address’) }},{{ facts_output | json_query(‘instances[].tags[].Name’) }}”


which gives me:

ok: [localhost] => {
   
"ansible_facts": {
       
"inject_ips": [
           
[
                "1.2.3.4",
                "1.2.3.4",
                "1.2.3.4",
                "1.2.3.4"
            ],
           
[
                "name1",
                "name2",
                "name3",
                "name4"
            ]
       
]
   
},
   
"changed": false
}



The point is to then use it like:


 
- name: execute to add the ips
    command
: “exec item[ip] item[name]
    run_once
: true
    with_items
:
     
- “{{ inject_ips }}”

Right now I'm splitting them by comma ","

Any suggestion is welcomed!

Help is very appreciated!

Thanks

David.





Kai Stian Olstad

unread,
May 26, 2018, 2:47:57 PM5/26/18
to ansible...@googlegroups.com
On 26.05.2018 04:32, David Villasmil wrote:
> Hellos guys,
>
> I have a result from ec2_instance_facts, and i can get the ip addresses
> like:
> - name: Get IP addresses from results
> set_fact:
> inject_ips: “{{ facts_output | json_query(‘instances[].
> public_ip_address’) }}”

<snip />

> I can also get the name with:
>
> - name: Get IP addresses from results
> set_fact:
> inject_ips: “{{ facts_output |
> json_query(‘instances[].tags[].Name’)
> }}”
>
> But i don't know how to concatenate them, i tried:
>
> - name: Get IP addresses from results
> set_fact:
> inject_ips: “{{ facts_output | json_query(‘instances[].
> public_ip_address’) }},{{ facts_output |
> json_query(‘instances[].tags[].Name
> ’) }}”

<snip />

> The point is to then use it like:
>
>
> - name: execute to add the ips
> command: “exec item[ip] item[name]”
> run_once: true
> with_items:
> - “{{ inject_ips }}”
>
> Right now I'm splitting them by comma ","
>
> Any suggestion is welcomed!

Why split them up, why not just use facts_output directly.

- name: execute to add the ips
command: “exec item.public_ip_address item.tags.Name
run_once: true
with_items:
- “{{ facts_output.instances }}”

--
Kai Stian Olstad

Kai Stian Olstad

unread,
May 26, 2018, 2:51:31 PM5/26/18
to ansible...@googlegroups.com
On 26.05.2018 20:47, Kai Stian Olstad wrote:
> Why split them up, why not just use facts_output directly.
>
> - name: execute to add the ips
> command: “exec item.public_ip_address item.tags.Name
> run_once: true
> with_items:
> - “{{ facts_output.instances }}”

A little correction
Reply all
Reply to author
Forward
0 new messages