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 }}"
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
creating dictionary with ansible
There are many options. For example,
give
Fit the quotation to your needs.
Example of a complete playbook for testing