2

I am trying to extract the IP addresses of the hosts that are part of two groups and use them in a .xml config file.

The problem I'm facing here is the output generated by the expression used into the jinja template. What I want to achieve is an output like 10.3.5.1,10.3.5.2,10.3.5.2 but instead of this I'm obtaining something like [u'10.3.5.1],[u'10.3.5.2],[u'10.3.5.2].

This is the expression used into the jinja template:

<member1>{{ (groups['group_one']+groups['group_two']) | map('extract',hostvars,'ansible_ip_addresses') | list | unique | join(',') }}</member1>

I have also tried some other filters but I could not get the expected result.

What filters should I use for this? (I couldn't find the right ones)

pandoJohn
  • 415
  • 1
  • 5
  • 8

1 Answers1

2

ansible_ip_addresses is a list, so you have a list (of hosts) with nested lists (with ip addresses).

You probably want to flatten it before applying unique and join:

{{ (groups['group_one']+groups['group_two']) | map('extract',hostvars,'ansible_ip_addresses') | list | sum(start=[]) | unique | join(',') }}

This will make a flat list with single ip-address as element, than take unique items and join them with comma.

Konstantin Suvorov
  • 3,996
  • 1
  • 12
  • 13