2017-10-14 124 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