# Passing a list in a list to loop

**URL:** <https://forum.ansible.com/t/passing-a-list-in-a-list-to-loop/22909>\
**Category:** Ansible Project\
**Created:** [October 28, 2016, 1:20am UTC](https://forum.ansible.com/t/passing-a-list-in-a-list-to-loop/22909 "2016-10-28T01:20:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil\_Watson](https://avatars.discourse-cdn.com/v4/letter/n/dbc845/32.png) [@Neil\_Watson](https://forum.ansible.com/u/Neil_Watson)\
**Post date:** [October 28, 2016, 1:20am UTC](https://forum.ansible.com/t/passing-a-list-in-a-list-to-loop/22909/1 "2016-10-28T01:20:30Z")

</div>

Greetings,

Below is an example of a playbook to manager some docker containers. I’ve built a data structure that my tasks can loop over. The trouble is that one task attribute, published\_ports requires a whole list, but when I pass is a list it only gets one element. How can I fix this?

vars:

# list of images and attributes

images:

- src: ../dns  
dest: /usr/src/  
path: /usr/src/dns  
name: my\_dns  
state: present

# list of containers and attributes

containers:

- name: my\_dns  
state: started  
published\_ports: # \<\<\< need to pass this list whole
- 53
- 53:53/udp

tasks:

- block:

- name: upload dockerfile and data  
copy:  
src: “{{ item.src }}”  
dest: “{{ item.dest }}”  
with\_items: “{{ images }}”

- name: build dns docker image  
docker\_image:  
path: “{{ item.path }}”  
name: “{{ item.name }}”  
state: “{{ item.state }}”  
with\_items: “{{ images }}”

- name: deploy container  
docker\_container:  
name: “{{ item.name }}”  
image: “{{ item.name }}”  
published\_ports: “{{ item.published\_ports }}” # \<\<\< How pass whole list?  
state: “{{ item.state }}”  
with\_items: “{{ containers }}”

---

<div class="post-metadata">

**Author:** ![Neil\_Watson](https://avatars.discourse-cdn.com/v4/letter/n/dbc845/32.png) [@Neil\_Watson](https://forum.ansible.com/u/Neil_Watson)\
**Post date:** [October 28, 2016, 1:53pm UTC](https://forum.ansible.com/t/passing-a-list-in-a-list-to-loop/22909/2 "2016-10-28T13:53:10Z")

</div>

Turns out published\_ports can take a string too, so use:

published\_ports: 53:53,53:53/udp

instead of

published\_ports:

- 53:53
- 53:53/udp

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 31, 2016, 4:33pm UTC](https://forum.ansible.com/t/passing-a-list-in-a-list-to-loop/22909/3 "2016-10-31T16:33:33Z")

</div>

It should be able to take both transparently, the argument is type ‘list’ which means that an actual list or a , separated string are both allowed as input.
