如何解决将字典的ansible列表转换为具有不同结构的json
我对如何将字典列表转换为json结构(需要将其作为变量发送到另一个模块中)感到迷茫
首先,我有一个字典列表,描述了有关我要创建的“团队”的所有信息(在awx / Tower中)。此列表详细说明了有关我的团队的大量信息。对于我需要进行的下一次更新,并非所有人都需要转换成JSON结构。
awxTeamsDefinition:
- { teamname: Team1,ldapgroupname: tower-team-1,description: This is tower team 1,organization: orgA }
- { teamname: Team2,ldapgroupname: tower-team-2,description: This is tower team 2,organization: orgA }
- { teamname: Team3,ldapgroupname: tower-team-3,description: This is tower team 3,organization: orgB }
- { teamname: Team4,ldapgroupname: tower-team-4,description: This is tower team 4,organization: orgB }
接下来,我需要提取一些信息并将其粘贴在如下所示的JSON结构中:
{
"Team1": {
"remove": true,"organization": "orgA","users": "cn=tower-team-1,cn=groups,cn=accounts,dc=domain,dc=com"
},"Team2": {
"remove": true,"users": "cn=tower-team-2,"Team3": {
"remove": true,"organization": "orgB","users": "cn=tower-team-3,"Team4": {
"remove": true,"users": "cn=tower-team-4,dc=com"
}
}
我需要一个可以传递到另一个模块的变量中的JSON结构。
这简直超出了我的烦人/ jinja欺骗能力。我不知道如何从字典中提取和提升每个json条目的索引(在本例中为“ teamname”),摆脱“ description”,并使用“ ldapgroupname”替换为cn结构。任何帮助,将不胜感激。谢谢!
解决方法
以下任务
- set_fact:
data1: "{{ data1|default({})|
combine({item.teamname:
{'remove': true,'organization': item.organization,'users': 'cn=' ~ item.ldapgroupname ~ users_append}}) }}"
loop: "{{ awxTeamsDefinition }}"
vars:
users_append: ",cn=groups,cn=accounts,dc=domain,dc=com"
- debug:
var: data1|to_nice_json
给予
data1|to_nice_json: |-
{
"Team1": {
"organization": "orgA","remove": true,"users": "cn=tower-team-1,dc=com"
},"Team2": {
"organization": "orgA","users": "cn=tower-team-2,"Team3": {
"organization": "orgB","users": "cn=tower-team-3,"Team4": {
"organization": "orgB","users": "cn=tower-team-4,dc=com"
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。