skip to Main Content

I have a simple list:

mpoints: ['/home/mp1', '/home/mp2', '/mnt/mp1', '/mnt/mp2']

I have a dict:

{ source: "/home/mp1", target: "/var/tmp/home/mp1", type: bind, read_only: no }

I need four entries (or four dicts) in a new_list (one dict per element on the list)

{ source: "/home/mp1", target: "/var/tmp/home/mp1", type: bind, read_only: no }
{ source: "/home/mp2", target: "/var/tmp/home/mp1", type: bind, read_only: no }
{ source: "/mnt/mp1", target: "/var/tmp/mnt/mp1", type: bind, read_only: no }
{ source: "/mnt/mp2", target: "/var/tmp/mnt/mp2", type: bind, read_only: no }

I have other variable too:

vars:
  - rootdir: '/var/tmp'

For any element in my list (mpoints) i need to add a new fully dict to a new_list:
So, something like this:

set_fact:
 new_list: "{{ new_list + [{ source: "{{ item }}", target: "{{ rootdir }}/{{ item }}", type: bind, read_only: no }]}}"
loop: {{ mpoint }}

To finally have all four items of my list(mpoints) in a new_list

mounts: "{{ new_list }}"

https://docs.ansible.com/ansible/latest/collections/community/docker/docker_container_module.html#ansible-collections-community-docker-docker-container-module

But clearly it doesn’t work like that.

Anybody ?

- community.docker.docker_container:
    name: nginx
    image: nginx
    state: started
    pull: true
    detach: yes
    tty: yes
    restart_policy: always
    ports: 80
    mounts:
      - { source: "/home/mp1", target: "/var/tmp/home/mp1", type: bind, read_only: no }
      - { source: "/home/mp2", target: "/var/tmp/home/mp1", type: bind, read_only: no }
      - { source: "/mnt/mp1", target: "/var/tmp/mnt/mp1", type: bind, read_only: no }
      - { source: "/mnt/mp2", target: "/var/tmp/mnt/mp2", type: bind, read_only: no }

2

Answers


  1. Chosen as BEST ANSWER
    - set_fact:
        new_list: "{{ new_list|default([]) + [{'source': '{{ rootdir }}' + item|string, 'target': item, 'type': 'bind', 'read_only': 'no' }] }}"
      loop: "{{ mpoints|flatten(1) }}"
    

    creating dictionary with ansible


  2. There are many options. For example,

      new_list_src: |
        {% for mpoint in mpoints %}
        - source: {{ mpoint }}
          target: {{ rootdir }}/{{ mpoint }}
          type: bind
          read_only: no
        {% endfor %}
      new_list: "{{ new_list_src|from_yaml }}"
    

    give

      new_list:
        - {read_only: false, source: /home/mp1, target: /var/tmp//home/mp1, type: bind}
        - {read_only: false, source: /home/mp2, target: /var/tmp//home/mp2, type: bind}
        - {read_only: false, source: /mnt/mp1, target: /var/tmp//mnt/mp1, type: bind}
        - {read_only: false, source: /mnt/mp2, target: /var/tmp//mnt/mp2, type: bind}
    

    Fit the quotation to your needs.


    Example of a complete playbook for testing

    - hosts: localhost
    
      vars:
    
        mpoints: [/home/mp1, /home/mp2, /mnt/mp1, /mnt/mp2]
        rootdir: /var/tmp
        new_list_src: |
          {% for mpoint in mpoints %}
          - source: {{ mpoint }}
            target: {{ rootdir }}/{{ mpoint }}
            type: bind
            read_only: no
          {% endfor %}
        new_list: "{{ new_list_src|from_yaml }}"
    
      tasks:
    
        - debug:
            var: new_list|to_yaml
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search