Passing a list in a list to loop

25 views
Skip to first unread message

Neil Watson

unread,
Oct 28, 2016, 9:40:04 AM10/28/16
to Ansible Project
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 }}"
 

Neil Watson

unread,
Oct 28, 2016, 9:53:10 AM10/28/16
to Ansible Project
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

Brian Coca

unread,
Oct 31, 2016, 12:33:53 PM10/31/16
to ansible...@googlegroups.com
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.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages