gpt4 book ai didi

list - Ansible - 根据另一个列表的值从一个列表中获取一个项目

转载 作者:行者123 更新时间:2023-12-04 10:51:50 24 4
gpt4 key购买 nike

我有一个像这样的站点信息列表:

sites:
- {host: host1, path: path1, db: db1 }
- {host: host2, path: path2, db: db2 }
- {host: host3, path: path3, db: db3 }

和列表中的数据库信息如下:
mysql_databases:
- { name: db1, encoding: utf8mb4, collation: utf8mb4_unicode_ci }
- { name: db2, encoding: utf8mb4, collation: utf8mb4_unicode_ci }
- { name: db3, encoding: utf8mb4, collation: utf8mb4_unicode_ci }

mysql_users:
- { name: db1, host: "%", password: pass1, priv: "db1.*:ALL", db: db1 }
- { name: db2, host: "%", password: pass2, priv: "db2.*:ALL", db: db2 }
- { name: db3, host: "%", password: pass3, priv: "db3.*:ALL", db: db3 }

我有一个使用上述信息创建凭据文件的角色。像这样的东西:
- name: Create file
template:
src: "credentials.conf.j2"
dest: "{{ item.path}}/{{ item.name }}.conf"
with_items: "{{ sites }}"

有没有什么方法可以在遍历 sites 时向模板发送所有三个列表中的相应信息?列表?

为了便于使用 geerlingguy 的 mysql 角色,这些列表的结构是这样的。

最佳答案

一种系统的方法是将 mysql_* 列表转换为字典。例如

- set_fact:
db_dict: "{{ db_dict|default({})|combine({item.name: item}) }}"
loop: "{{ mysql_databases }}"
- set_fact:
users_dict: "{{ users_dict|default({})|combine({item.name: item}) }}"
loop: "{{ mysql_users }}"
- template:
src: credentials.conf.j2
dest: "{{ item.path}}/{{ item.db }}.conf"
loop: "{{ sites }}"

然后,使用正确结构化的数据进行操作是微不足道的。例如模板
$ cat credentials.conf.j2
{% for k,v in db_dict[item.db].iteritems() %}
{{ k }}: {{ v }}
{% endfor %}

{% for k,v in users_dict[item.db].iteritems() %}
{{ k }}: {{ v }}
{% endfor %}


$ cat path1/db1.conf 
collation: utf8mb4_unicode_ci
name: db1
encoding: utf8mb4

host: %
password: pass1
db: db1
name: db1
priv: db1.*:ALL

可以删除属性 name从字典。例如
- set_fact:
db_dict: "{{ db_dict|default({})|
combine({item.name: item|dict2items|
rejectattr('key', 'match', 'name')|
list|items2dict}) }}"
loop: "{{ mysql_databases }}"
- debug:
var: db_dict

- set_fact:
users_dict: "{{ users_dict|default({})|
combine({item.name: item|dict2items|
rejectattr('key', 'match', 'name')|
list|items2dict}) }}"
loop: "{{ mysql_users }}"
- debug:
var: users_dict


"db_dict": {
"db1": {
"collation": "utf8mb4_unicode_ci",
"encoding": "utf8mb4"
},
"db2": {
"collation": "utf8mb4_unicode_ci",
"encoding": "utf8mb4"
},
"db3": {
"collation": "utf8mb4_unicode_ci",
"encoding": "utf8mb4"
}
}


"users_dict": {
"db1": {
"db": "db1",
"host": "%",
"password": "pass1",
"priv": "db1.*:ALL"
},
"db2": {
"db": "db2",
"host": "%",
"password": "pass2",
"priv": "db2.*:ALL"
},
"db3": {
"db": "db3",
"host": "%",
"password": "pass3",
"priv": "db3.*:ALL"
}
}

然后,
- template:
src: credentials.conf.j2
dest: "{{ item.path}}/{{ item.db }}.conf"
loop: "{{ sites }}"


$ cat path1/db1.conf 
collation: utf8mb4_unicode_ci
encoding: utf8mb4

host: %
password: pass1
db: db1
priv: db1.*:ALL

关于list - Ansible - 根据另一个列表的值从一个列表中获取一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59448722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com