Home > Mobile >  Create dynamic dictionaries with ansible
Create dynamic dictionaries with ansible

Time:12-28

I have a simples 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 }

CodePudding user response:

- 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

CodePudding user response:

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
  • Related