我有一个列表,需要根据列表List:input 中的特定单词将该列表转换/过滤为字典
[
"[profile appdev]\r\nArn: aaa.dev:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
"[profile applead]\r\nArn: aaa.techlead:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
"[profile appprod]\r\nArn: aaa.techlead:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
"[profile apppadmin]\r\nArn: aaa.techlead:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
"[profile appsupport]\r\nArn: aaa.dev:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
"[profile appguest]\r\nArn: aaa.techlead:433:iam\r\nRegion: ap.southeast3"
]
字典:输出
{
[profile appdev]:"profile appdev]\r\nArn: aaa.dev:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
[profile applead]:"[profile applead]\r\nArn: aaa.techlead:333:iam\r\nRegion: ap.southeast2\r\n\r\n"
[profile appprod]:"[profile appprod]\r\nArn: aaa.techlead:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
[profile apppadmin]:"[profile apppadmin]\r\nArn: aaa.techlead:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
[profile appsupport]:"[profile appsupport]\r\nArn: aaa.dev:333:iam\r\nRegion: ap.southeast2\r\n\r\n",
[profile appguest]:"[profile appguest]\r\nArn: aaa.techlead:433:iam\r\nRegion: ap.southeast3"
}
是否可以在 Ansible 中实现
试试这个
- set_fact:
dict1: "{{ dict(keys|zip(list1)) }}"
vars:
regex: '(\[.*\])(.*)'
replace: '\1'
keys: "{{ list1|map('regex_replace', regex, replace)|list }}"
给定变量list1 中的列表,创建一个键列表。例如,使用map和regex_replace来创建列表键。然后使用dict和zip创建字典。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句