使用魔术变量中的变量通过ansible编写/ etc / hosts

时间:2018-07-06 08:33:21

标签: ansible cluster-computing

我正试图通过ansible将文件/ etc / hosts写入群集。我想在魔术变量中使用group_vars变量。这是一个工作示例,其中我使用 ansibe_default_ipv4 而没有group_vars变量:

    - name: Add hosts to /etc/hosts
      lineinfile:
      args:
         dest: "/etc/hosts"
         regexp: "{{ hostvars[item]['ansible_default_ipv4']['address'] }}"
         line: "{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{ hostvars[item]['ansible_fqdn'] }}   {{ hostvars[item]['ansible_fqdn'].split('.')[0] }}"
      with_items: "{{ play_hosts }}"
      tags: ha-cluster

但是我想使用 ansible _ {{cluster_interface}} 之类的东西来代替 ansible_default_ipv4 ,其中 cluster_interface 是group_vars中的变量,例如 eth0 eth1 。通过为不同群集使用某些接口来使角色更加灵活。

这不是工作示例:

    - name: Add hosts to /etc/hosts
      lineinfile:
      args:
         dest: "/etc/hosts"
         regexp: "{{ hostvars[item]['ansible_default_ipv4']['address'] }}"
         line: "{{ hostvars[item]['ansible_{{ cluster_ineterface }}']['ipv4']['address'] }} {{ hostvars[item]['ansible_fqdn'] }}   {{ hostvars[item]['ansible_fqdn'].split('.')[0] }}"
      with_items: "{{ play_hosts }}"
      tags: ha-cluster

错误:

fatal: [node1.linux.cluster.test]: FAILED! => {
"failed": true, 
"msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. 
The error was: 'dict object' has no attribute 'ansible_{{ cluster_interface }}'\n
\nThe error appears to have been in '/home/name/.ansible/roles/ha-cluster/tasks/main.yml': 
line 36, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n
\nThe offending line appears to be:\n\n\n- name: Add hosts to /etc/hosts\n  ^ here\n"
}

Ansible版本:2.2.1.0-2

主机:Debian 9.4

甚至可以在魔术变量中使用group_vars吗? 谢谢!

0 个答案:

没有答案