ansible - Ansible:如何根据列表中的特定单词将列表转换/过滤为字典
问题描述
我有一个列表,需要根据列表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创建字典。
推荐阅读
- svg - 如何以编程方式从目录中的多个 svg 中提取“d”属性?
- angular - 使用 Angular 应用程序在基于 Web 的 ms office 中打开 word 文档文件
- flutter - 使用包前缀导入在 Flutter 中不起作用
- django - Django orm 返回时区 0,而我想要时区规范
- c# - PropertyDescriptor.SetValue 异常调用堆栈
- arrays - 将范围分成间隔并将其输出到数组中
- android - 带有格式化文本的 AutoSize 标签在 Android 上不起作用
- wpf - 我可以通过 ListView 项在 WPF 中与 ImageAwesome 进行图标绑定吗?
- python - Python循环多个数据框
- reactjs - [TYPESCRIPT]:react/require-default-props 在我的 .eslintrc 配置中无法正常工作