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

3,029 views
Skip to first unread message

trupti mali

unread,
Sep 14, 2015, 2:47:01 AM9/14/15
to Ansible Project
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.

Brian Coca

unread,
Sep 14, 2015, 12:14:51 PM9/14/15
to Ansible Project
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']) }}"


--
Brian Coca

trupti mali

unread,
Sep 15, 2015, 3:12:05 AM9/15/15
to Ansible Project
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

Brian Coca

unread,
Sep 15, 2015, 9:52:55 AM9/15/15
to Ansible Project
I did not realize, you are self referencing exceptions, that won't work

--
Brian Coca

trupti mali

unread,
Sep 16, 2015, 9:24:38 AM9/16/15
to Ansible Project
Oh well I guessed so. Then how to solve this problem of concatenating values by iterating in a with_items loop?

Olve Hansen

unread,
Sep 17, 2015, 6:42:49 AM9/17/15
to Ansible Project
- 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 :-)

HTH
-- 
Olve
Reply all
Reply to author
Forward
0 new messages