ansible中的group_names变量

时间:2017-10-14 06:34:43

标签: ansible ansible-inventory

我在执行这个剧本时遇到了一些问题:

- hosts: all
  connection: local
  tasks:
  - template: src=/etc/ansible/{{group_names}}/common.j2 dest=/etc/ansible/configs/{{inventory_hostname}}.txt
    name: create common config snippets

我得到的错误是:

fatal: [R1]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to find '/etc/ansible/[u'ios']/common.j2' in expected paths."}
fatal: [R2]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to find '/etc/ansible/[u'ios1']/common.j2' in expected paths."}

以下是我的小组:

/etc/ansible# cat hosts | grep ios           
[ios]
[ios1]

这是我的common.j2文件:

/etc/ansible# ls ios1/
common.j2


/etc/ansible# ls ios/ 
common.j2

有人可以详细说明为什么group_names会返回[u'group_names]吗?

1 个答案:

答案 0 :(得分:2)

因为group_names列表(这就是它被[ ]包围的原因) - 主机可以属于多个组。

你需要决定,你的目标是什么:

  • 如果要包含所有组的文件,则必须添加循环:

    - hosts: all
      connection: local
      tasks:
        - name: create common config snippets
          template:
            src: /etc/ansible/{{item}}/common.j2
            dest: /etc/ansible/configs/{{inventory_hostname}}.txt
          with_items: "{{group_names}}"
    
  • 如果您想添加单个组,可以引用单个元素(group_names[0]),但这似乎不切实际......