【发布时间】:2020-08-16 12:19:53
【问题描述】:
我对如何将字典列表转换为需要作为变量发送到另一个模块的 json 结构有点茫然
首先,我有一个 dict 列表,其中描述了有关我正在创建的“团队”的所有信息(在 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,
"organization": "orgA",
"users": "cn=tower-team-2,cn=groups,cn=accounts,dc=domain,dc=com"
},
"Team3": {
"remove": true,
"organization": "orgB",
"users": "cn=tower-team-3,cn=groups,cn=accounts,dc=domain,dc=com"
},
"Team4": {
"remove": true,
"organization": "orgB",
"users": "cn=tower-team-4,cn=groups,cn=accounts,dc=domain,dc=com"
}
}
我需要将这个 JSON 结构放在一个可以传递给另一个模块的变量中。
这简直超出了我的 ansible / jinja 诡计能力。我不知道如何从 dict 中提取并提升每个 json 条目的索引(在本例中为“teamname”),去掉“description”,并使用“ldapgroupname”替换为 cn 结构.任何帮助,将不胜感激。谢谢!
【问题讨论】: