Yaml Ansible AnsibleUndefinedVariable无属性
我有以下文件vars/main.ymlYaml Ansible AnsibleUndefinedVariable无属性,yaml,ansible,Yaml,Ansible,我有以下文件vars/main.yml testconfig: - {hostname: router123, example: no ip cef} cisco_891_l2interfaces: - FastEthernet0 - FastEthernet1 - FastEthernet2 - FastEthernet3 - FastEthernet4 - FastEthernet5 - FastEthernet6 - FastEthernet7 euvar: - {
testconfig:
- {hostname: router123, example: no ip cef}
cisco_891_l2interfaces:
- FastEthernet0
- FastEthernet1
- FastEthernet2
- FastEthernet3
- FastEthernet4
- FastEthernet5
- FastEthernet6
- FastEthernet7
euvar:
- {dc1: "1.1.1.1", dc2: "1.2.2.2"}
任务main.yml
- name: Generate configuration files for DMVPN router
template: src=router.j2 dest=/lib/python2.7/site-packages/ansible/RTR-TEMPLATE/bla/bla.txt
with_items: testconfig
路由器.j2
{{item.example}}
!
boot-start-marker
boot-end-marker
!
{{euvar.dc1}}
TASK: [router | Generate configuration files for DMVPN router] ****************
fatal: [localhost] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'list object' has no attribute 'dc1'", 'failed': True}
fatal: [localhost] => {'msg': 'One or more items failed.', 'failed': True, 'changed': False, 'results': [{'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'list object' has no attribute 'dc1'", 'failed': True}]}
FATAL: all hosts have already failed -- aborting
这给了我一个错误,我不确定如何从router.j2引用euvar.dc1
TASK: [router | Generate configuration files for DMVPN router] ****************
fatal: [localhost] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'list object' has no attribute 'dc1'", 'failed': True}
fatal: [localhost] => {'msg': 'One or more items failed.', 'failed': True, 'changed': False, 'results': [{'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'list object' has no attribute 'dc1'", 'failed': True}]}
FATAL: all hosts have already failed -- aborting
您将
euvar
定义为一个目录列表。但您正试图直接访问第一个列表项。从我的感觉来看,你只是想要一个这样的口述:
euvar: {dc1: "1.1.1.1", dc2: "1.2.2.2"}
或者对于那些喜欢可读性的人:
euvar:
dc1: 1.1.1.1
dc2: 1.2.2.2
然后您将能够以{{euvar.dc1}}
的身份访问它
如果您真的想定义一个dict列表,那么您可以像这样访问它@ᴳᵁᴵᴰᴼ 已经对以下问题发表了评论:{{euvar[0].dc1}
请同时查看Ansibles和。变量的名称表明您正在使用Ansibles。是否也应该循环使用
euvar
(如testconfig
)?还是只需要选择第一行的值?(在后一种情况下,它是{{euvar[0].dc1}}
)基本上我有特定于区域的IP地址。我想在region=EU时从dc1 euvar获取值,在region=us时使用usvar dc1。