英文:
Ansible - Create multiple mysql users with multiple hosts
问题
我有以下示例变量:
applications:
app1:
app_user:app1
db_user:db_app1
db_pass:abc123
hosts:
- 10.0.0.25
- localhost
- 127.0.0.1
app2:
app_user:app2
db_user:db_app2
db_pass:abc345
hosts:
- 10.0.0.26
- localhost
- 127.0.0.1
现在我想使用mysql_users模块创建用户(使用db_user/db_pass/hosts)。
目前我不知道如何解析applications字典并传递db_user/db_pass,另外还要为applications.app*.hosts中的每个主机重复。
原则上,我需要自动生成以下列表:
- { user: "db_app1", password: "abc123" , host: "10.0.0.25" }
- { user: "db_app1", password: "abc123" , host: "localhost" }
- { user: "db_app1", password: "abc123" , host: "127.0.0.1" }
- { user: "db_app2", password: "abc345" , host: "10.0.0.26" }
- { user: "db_app2", password: "abc345" , host: "localhost" }
- { user: "db_app2", password: "abc345" , host: "127.0.0.1" }
解决这个难题的任何提示都将不胜感激。
提前致谢。
英文:
I'm having the following example variables:
applications:
app1:
app_user: app1
db_user: db_app1
db_pass: abc123
hosts:
- 10.0.0.25
- localhost
- 127.0.0.1
app2:
app_user: app2
db_user: db_app2
db_pass: abc345
hosts:
- 10.0.0.26
- localhost
- 127.0.0.1
Now I want to create the users with the mysql_users module (with db_user/db_pass/hosts).
Right now I've no clue how to parse the applications dictionary and handover db_user/db_pass and additionally repeat for each host in applications.app*.hosts
In principle I would need the following list to be generated automatically:
- { user: "db_app1", password: "abc123" , host: "10.0.0.25" }
- { user: "db_app1", password: "abc123" , host: "localhost" }
- { user: "db_app1", password: "abc123" , host: "127.0.0.1" }
- { user: "db_app2", password: "abc345" , host: "10.0.0.26" }
- { user: "db_app2", password: "abc345" , host: "localhost" }
- { user: "db_app2", password: "abc345" , host: "127.0.0.1" }
Any hints to solve the puzzle would be very much appreciated.
Thx in advance
答案1
得分: 1
使用 dict2items 将字典转换为列表,并与 subelements 一起迭代。例如,
- debug:
msg: "user: {{ user }} password: {{ pswd }} host: {{ item.1 }}"
loop: "{{ applications|dict2items|subelements('value.hosts') }}"
vars:
user: "{{ item.0.value.db_user }}"
pswd: "{{ item.0.value.db_pass }}"
给出了缩略
msg: 'user: db_app1 password: abc123 host: 10.0.0.25'
msg: 'user: db_app1 password: abc123 host: localhost'
msg: 'user: db_app1 password: abc123 host: 127.0.0.1'
msg: 'user: db_app2 password: abc345 host: 10.0.0.26'
msg: 'user: db_app2 password: abc345 host: localhost'
msg: 'user: db_app2 password: abc345 host: 127.0.0.1'
完整的测试剧本示例
- hosts: localhost
vars:
applications:
app1:
app_user: app1
db_user: db_app1
db_pass: abc123
hosts:
- 10.0.0.25
- localhost
- 127.0.0.1
app2:
app_user: app2
db_user: db_app2
db_pass: abc345
hosts:
- 10.0.0.26
- localhost
- 127.0.0.1
tasks:
- debug:
msg: "user: {{ user }} password: {{ pswd }} host: {{ item.1 }}"
loop: "{{ applications|dict2items|subelements('value.hosts') }}"
loop_control:
label: "{{ item.0.key }}"
vars:
user: "{{ item.0.value.db_user }}"
pswd: "{{ item.0.value.db_pass }}"
英文:
Use dict2items to convert the dictionary to a list and iterate with subelements. For example,
- debug:
msg: "user: {{ user }} password: {{ pswd }} host: {{ item.1 }}"
loop: "{{ applications|dict2items|subelements('value.hosts') }}"
vars:
user: "{{ item.0.value.db_user }}"
pswd: "{{ item.0.value.db_pass }}"
gives abridged
msg: 'user: db_app1 password: abc123 host: 10.0.0.25'
msg: 'user: db_app1 password: abc123 host: localhost'
msg: 'user: db_app1 password: abc123 host: 127.0.0.1'
msg: 'user: db_app2 password: abc345 host: 10.0.0.26'
msg: 'user: db_app2 password: abc345 host: localhost'
msg: 'user: db_app2 password: abc345 host: 127.0.0.1'
<hr>
<sup>
Example of a complete playbook for testing
- hosts: localhost
vars:
applications:
app1:
app_user: app1
db_user: db_app1
db_pass: abc123
hosts:
- 10.0.0.25
- localhost
- 127.0.0.1
app2:
app_user: app2
db_user: db_app2
db_pass: abc345
hosts:
- 10.0.0.26
- localhost
- 127.0.0.1
tasks:
- debug:
msg: "user: {{ user }} password: {{ pswd }} host: {{ item.1 }}"
loop: "{{ applications|dict2items|subelements('value.hosts') }}"
loop_control:
label: "{{ item.0.key }}"
vars:
user: "{{ item.0.value.db_user }}"
pswd: "{{ item.0.value.db_pass }}"
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论