2017-10-14 93 views
1

我运行的一些问题,当我执行这个剧本:GROUP_NAMES变量ansible

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

+0

'group_names' [是一个列表(https://docs.ansible.com/ansible/latest/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)。它没有意义的,它代替在这样的一个文件名。 – larsks

+0

我是新来ansible,我不知道,并感谢这么多提示答案。 – NANIS

回答

1

因为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]),但这似乎并不实际...

+0

你们是惊人的。 – NANIS