如何循环浏览清单文件?

时间:2019-07-15 23:30:08

标签: ansible

我想在模板模块中的主机清单文件中循环

可用2.8.1

文件主机 :

[servers]
test1 ansible_host=10.x.x.1 number=1
test2 ansible_host=10.x.x.2 number=2

- name: edit servers' file
    loop: "{{ groups['all'] }}"
    template:
      vars:
        peer: "{{ number }}"
      src: conf/ntp.j2
      dest: /etc/test.conf

I expected template j2 file with line:
{{ peer }}
replaced by number1 on test1
replaced by number2 on test2

fatal: [Server2]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'number' is undefined"}

1 个答案:

答案 0 :(得分:0)

您可以使用下面的剧本。使用清单主机名/ ansible_hostname。

- name: edit servers file
  template:
    vars:
      peer: "{{ hostvars[inventory_hostname].number }}"
    src: conf/ntp.j2
    dest: /etc/test.conf