Ansible:创建相同的列表项,其中数字取决于另一个变量的值
Ansible: Create identical list items where number depending on value of another variable
我想为 Ansible URI 模块编写主体。
因此我需要一个字典,我试图用 set_fact.
创建
本词典应包含具有相同 key/value 对的项目列表。
key/value 对必须添加的频率在另一个变量中定义,比方说 number_of_functions.
当 number_of_functions 为 5 时,预期结果应包含相同的 key/value 对 5 次:
composed_body:
functions:
- functionType: something
- functionType: something
- functionType: something
- functionType: something
- functionType: something
然后我将在 URI 正文中使用 composed_body 变量和“body_format: json”,这将给出:
{
"functions": [
{
"functionType": "something"
},
{
"functionType": "something"
},
{
"functionType": "something"
},
{
"functionType": "something"
},
{
"functionType": "something"
}
]
}
我尝试使用 jinja2/range/regex 替换,但我从未得到有效的 YAML/JSON 结构。
知道如何根据 number_of_functions 的值创建列表条目吗?
例如
- set_fact:
composed_body: "{{ _composed_body|from_yaml }}"
vars:
number_of_functions: 5
_composed_body: |-
functions:
{% for i in range(number_of_functions) %}
- functionType: something
{% endfor %}
给予
composed_body:
functions:
- functionType: something
- functionType: something
- functionType: something
- functionType: something
- functionType: something
我想为 Ansible URI 模块编写主体。 因此我需要一个字典,我试图用 set_fact.
创建本词典应包含具有相同 key/value 对的项目列表。 key/value 对必须添加的频率在另一个变量中定义,比方说 number_of_functions.
当 number_of_functions 为 5 时,预期结果应包含相同的 key/value 对 5 次:
composed_body:
functions:
- functionType: something
- functionType: something
- functionType: something
- functionType: something
- functionType: something
然后我将在 URI 正文中使用 composed_body 变量和“body_format: json”,这将给出:
{
"functions": [
{
"functionType": "something"
},
{
"functionType": "something"
},
{
"functionType": "something"
},
{
"functionType": "something"
},
{
"functionType": "something"
}
]
}
我尝试使用 jinja2/range/regex 替换,但我从未得到有效的 YAML/JSON 结构。
知道如何根据 number_of_functions 的值创建列表条目吗?
例如
- set_fact:
composed_body: "{{ _composed_body|from_yaml }}"
vars:
number_of_functions: 5
_composed_body: |-
functions:
{% for i in range(number_of_functions) %}
- functionType: something
{% endfor %}
给予
composed_body:
functions:
- functionType: something
- functionType: something
- functionType: something
- functionType: something
- functionType: something