Python 将Ansible中的列表项映射到不同的值
假设我有以下输入结构Python 将Ansible中的列表项映射到不同的值,python,ansible,devops,Python,Ansible,Devops,假设我有以下输入结构 domain: topdomain.com subdomains: - name: foo - name: bar 我想要这个输出 domains: - name: foo.topdomain.com - name: bar.topdomain.com 可以在Python中实现的东西 domain = 'topdomain.com' items = [1, 2, 3, 4, 5] squared = list(map(lambda x: x**2, i
domain: topdomain.com
subdomains:
- name: foo
- name: bar
我想要这个输出
domains:
- name: foo.topdomain.com
- name: bar.topdomain.com
可以在Python中实现的东西
domain = 'topdomain.com'
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
subdomains = [
{
'name': 'foo'
},
{
'name': 'bar'
}
]
domains = list(map(lambda sd: {'name': sd['name'] + '.' + domain}, subdomains))
print(domains)
如何使用过滤器在Ansible中实现这一点?理想情况下实现任何定制或特殊功能
注意:我不是在寻找如何将一个对象列表映射到一个字符串列表,就像使用
map(attribute='name')|map('regex_replace',…')
所做的那样,尽管它需要一点小技巧(即从下面的到| json |)到,以下剧本满足您的要求。请注意,由于使用了
结果是:
$ ansible-playbook play.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Show the result] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"domains": [
{
"name": "foo.topdomain.com"
},
{
"name": "bar.topdomain.com"
}
]
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook play.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Show the result] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"domains": [
{
"name": "foo.topdomain.com"
},
{
"name": "bar.topdomain.com"
}
]
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0