I have a clients variable that is defined like this:
clients:
- name: 'acme'
production: False
db_pwd: '123'
- name: 'example'
production: True
db_pwd: '456'
I need to process this list and come up with another list for users in the mongo database
users:
- database: 'client_acme',
name: 'client_acme',
password: '123'
roles: ['readWrite', 'dbAdmin']
- database: 'client_example',
name: 'client_example',
password: '456'
roles: ['readWrite', 'dbAdmin']
How would I go about this?
this task does the job:
- name: "Test1"
hosts: localhost
vars:
clients:
- name: 'acme'
production: False
db_pwd: '123'
- name: 'example'
production: True
db_pwd: '456'
tasks:
- name: set_fact
set_fact:
users: "{{ users | default([]) + [ ({ 'database': newname, 'name': newname, 'password': password, 'roles': ['readWrite', 'dbAdmin'] })] }}"
loop: "{{ clients }}"
vars:
newname: client_{{ item.name }}
password: "{{ item.db_pwd }}"
- name: debug users
debug:
var: users
result:
ok: [localhost] =>
users:
- database: client_acme
name: client_acme
password: '123'
roles:
- readWrite
- dbAdmin
- database: client_example
name: client_example
password: '456'
roles:
- readWrite
- dbAdmin