Ansible如何在模板的文件中循环



需要逐个循环所有主机的源文件。

- hosts: epson*
become: yes
tasks:
- name: replace id
vars:
id: abc
template:
src: epson1.j2
dest: /home/epson.config
HOSTS FILE
[epson1]
1.1.1.1
[epson2]
1.1.1.1
[epson3]
1.1.1.1
and many more
epson1.j2
create element edge0 {
state="ENABLED"
id="{{ id }}"}
epson2.j2
create element edge1 {
state="ENABLED"
id="{{ id }}"}

我有很多模板文件,比如epson1.j2、epson2.j2等等。现在我可以为1个主机和1个文件进行模板变量替换。如何处理所有主机的所有文件。

like - host:epson1, src: epson1.j2, dest: /home/epson.config
host:epson2, src: epson2.j2, dest: /home/epson.config
host:epson3, src: epson3.j2, dest: /home/epson.config
need looping inside src for every hosts

您应该能够通过使用inventory_hostname魔术变量来实现这一点。

- hosts: epson*
become: yes
tasks:
- name: replace id
vars:
id: abc
template:
src: {{ inventory_hostname }}.j2
dest: /home/epson.config

播放将为每个主机运行一次,并且将使用正确的.j2

相关内容

  • 没有找到相关文章

最新更新