set fact - concatenate to the same list -iterating over with items

Hi,
I am facing one issue here. I want to loop over the existing list using with items and create another list using previous list values.

  • name: collect iptables exception lines

set_fact:

exceptions: "{{ exceptions | union([ ‘-A INPUT -p tcp --source {{ item }} --dport 3306 -j ACCEPT’ ,

‘-A INPUT -p tcp --source {{ item }} --dport 4567 -j ACCEPT’]) }}"

with_items: web_ips

When I run above playbook, i get err

fatal: [db_node1] => Failed to template {{ exceptions | union([‘-A INPUT -p tcp --source {{ item }} --dport 3306 -j ACCEPT’,‘-A INPUT -p tcp --source {{ item }} --dport 4567 -j ACCEPT’) }}: an unexpected type error occurred. Error was coercing to Unicode: need string or buffer, list found

my ansible version is 1.9.3.

moustaches don't stack:

  set_fact:

     exceptions: "{{ exceptions | union([ '-A INPUT -p tcp --source '
+ item + ' --dport 3306 -j ACCEPT' ,

                                                             '-A INPUT
-p tcp --source ' + item + ' --dport 4567 -j ACCEPT']) }}"

Thanks for the response Brian. I tried your solution but the same error sticks around

Failed to template {{ exceptions | union([‘-A INPUT -p tcp --source ‘+ item + ’ --dport 3306 -j ACCEPT’,’-A INPUT -p tcp --source ‘+ item +’ --dport 4567 -j ACCEPT’,‘-A INPUT -p tcp --source ‘+ item +’ --dport 4568 -j ACCEPT’]) }}: an unexpected type error occurred. Error was coercing to Unicode: need string or buffer, list found

Its refusing to accept list contents inside union function which is strange.

~Trupti

I did not realize, you are self referencing exceptions, that won't work

Oh well I guessed so. Then how to solve this problem of concatenating values by iterating in a with_items loop?

- set_fact:
    zk_connection_: >
      {%- set servers = [] %}
      {%- for host in groups['zk_servers'] %}
      {%-    set _ = servers.append(hostvars[host]['ansible_' +iface]['ipv4']['address']) %}
      {%- endfor %}
      {{- servers -}}

Is how I have done it. I got some help on IRC to solve the initials, and now I use this jinja2 inline template instead of doing it through a proper template and load the result as a variable. Guess you can swap the 'groups[‘zk_servers’] with your variable and the append part you will have to figure out yourself :slight_smile:

HTH