将字符串附加到存储在变量中的每个列表项



开发者社区您好!

我目前正在开发一些Ansible操作手册来管理Citrix NetScaler配置,并希望获得以下方面的帮助。我在YAML文件中定义了以下数据结构:

prefix_header:                         "foo"
prefix_trailer:                        "bar"
nsapp_cs_vserver:
- name:                              "testwebvserver-4_SSL_443"
policybindings:
- policyname:                    "TO_testwebservice-3"
priority:                      "100"
- policyname:                    "To-be-deleted"
priority:                      "110"

我正试图找到一种简单的方法,将"policybindings"列表变量的内容动态转换为以下格式:(我想将头和尾前缀附加到"policyname"的实际值(

policybindings:
- policyname:                    "foo_TO_testwebservice-3_bar"
priority:                      "100"
- policyname:                    "foo_To-be-deleted_bar"
priority:                      "110"

我想使用带有头和尾前缀的策略名称来调用netscaler_cs_vserver Ansible模块来配置内容切换。

- name: "Bind CS policy(ies) to CS vServer(s) on ACTIVE node"
netscaler_cs_vserver:
name: "{{ prefix_header }}{{ item.name }}{{ prefix_trailer}}"
policybindings: "{{ item.policybindings }}"
register: bind_nsapp_cs_policy_result
loop: "{{ nsapp_cs_vserver }}"

有人能告诉我们实现这一目标的正确有效的方法是什么吗?

非常感谢!

可以循环include_tasks来处理嵌套列表。例如文件

shell> cat convert-list.yml
- set_fact:
policybindings: []
- set_fact:
policybindings: "{{ policybindings +
[item|combine({'policyname':
item.policyname|
regex_replace( myregex, myreplace)})] }}"
loop: "{{ outer_item.policybindings }}"
vars:
myregex: '^(.*)$'
myreplace: "{{ prefix_header ~ '_\1_' ~ prefix_trailer }}"
- set_fact:
nsapp_cs_vserver2: "{{ nsapp_cs_vserver2|default([]) +
[outer_item|combine({'policybindings': policybindings})] }}"

包含在"外循环"任务中

- include_tasks: convert-list.yml
loop: "{{ nsapp_cs_vserver }}"
loop_control:
loop_var: outer_item
- debug:
var: nsapp_cs_vserver2

给出

"nsapp_cs_vserver2": [
{
"name": "testwebvserver-4_SSL_443", 
"policybindings": [
{
"policyname": "foo_TO_testwebservice-3_bar", 
"priority": "100"
}, 
{
"policyname": "foo_To-be-deleted_bar", 
"priority": "110"
}
]
}
]

相关内容

  • 没有找到相关文章

最新更新