【发布时间】:2020-07-09 00:14:59
【问题描述】:
如何嵌套 with_filetree 和循环?
这是我使用块的尝试:
- name: Deploy
hosts: all
connection: ssh
become: true
vars:
instances:
abcd:
admin_port: 12345
tasks:
- name: Template cfg
vars:
file_path: "{{ item.path }}"
block:
- name: Templating config files
vars:
instance: "{{ item.value }}"
template:
src: "config-templates/{{ file_path }}"
dest: "{{ install_dir }}/{{ instance.name }}/"
loop: "{{ instances | dict2items }}"
with_filetree: "config-templates"
when: item.state == 'file'
但是 ansible 抱怨:
ERROR! 'with_filetree' is not a valid attribute for a Block
我一定遗漏了一些明显的东西,但我不知道该怎么做。使用 with_nested/with_cartesian 似乎不起作用。
请帮忙。
【问题讨论】:
-
你不能遍历一个块。实际上,您实际上不能在包含文件的 ansible appart 循环中真正嵌套循环。这可能是这里的解决方案。警告:您必须使用
loop_control: {loop_var: someothervar}来区分内部和外部循环变量,否则它们将在同一个item上发生冲突。 -
@Zeitounator 感谢您向我展示了一种方法。不确定您提到的是否是
include_tasks,但它的工作方式与您描述的一样。 -
我就是这个意思。同时,经过短暂的睡眠后,我能够通过单个任务测试另一个(我相信......)工作解决方案。请参阅下面的答案。
标签: ansible