Ansible中的group_names变量

4

当我执行这个playbook时,遇到了一些问题:

- 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'] 吗?


group_names是一个列表,详见魔术变量和如何访问其他主机的信息。在文件名中这样替换是没有意义的。 - larsks
我对ansible很新,不知道这个,非常感谢您的及时回答。 - NANIS
1个回答

6

因为group_names是一个列表(所以被[ ]包围)—— 一个主机可以属于多个组。

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

  • If you wanted to include files for all groups, you have to add a loop:

    - 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}}"
    
  • If you wanted to add a single group, you could refer to a single element (group_names[0]), but that doesn't seem practical...


你们太棒了。 - NANIS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接