group_names ansible 中的变量
group_names variable in 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
returns [u'group_names]
吗?
因为group_names
一个列表(这就是它被[ ]
包围的原因)——一个主机可以属于多个组。
你需要决定,你的 objective:
如果你想包含所有组的文件,你必须添加一个循环:
- 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]
),但这似乎不实用...
我在执行此剧本时 运行 遇到一些问题:
- 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
returns [u'group_names]
吗?
因为group_names
一个列表(这就是它被[ ]
包围的原因)——一个主机可以属于多个组。
你需要决定,你的 objective:
如果你想包含所有组的文件,你必须添加一个循环:
- 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]
),但这似乎不实用...