我需要将几个带有文件内容的词典添加到一个列表中,以便进一步与URI模块一起使用. 我现在正在手动操作,就像这样:
ansible.builtin.uri:
method: POST
body_format: json
body:
actions:
- action: create
file_path: ".gitlab-ci.yml"
content: "{{ lookup('file', '.gitlab-ci.yml') }}"
- action: create
file_path: "README.md"
content: "{{ lookup('file', 'README.md') }}"
我希望使此任务具有通用性,这样我就不必硬指定文件的名称:
ansible.builtin.set_fact:
files:
- .gitlab-ci.yml
- README.md
ansible.builtin.uri:
method: POST
body_format: json
body:
actions: ["
{% for fs in files -%}
{
'action': 'create',
'file_path': '{{ fs }}',
'content': '{{ lookup('file', fs) }}'
}{% if not loop.last %},{% endif %}
{%- endfor %}"
]
因此,文件的内容存储在Actions[*]Content变量中,但仅作为字符串存储,因为文件包含标记.如果将lookup替换为"Hello World"行,则会正常生成json.
你能告诉我如何使用loop
和lookup
来保存actions
变量中有content
键的文件的内容吗?