Ansible AnsibleUndefinedVariable没有属性

时间:2016-01-08 16:51:23

标签: yaml ansible

我有以下文件vars / main.yml

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

router.j2

{{item.example}}

!
boot-start-marker
boot-end-marker
!

{{euvar.dc1}}

这给了我错误,我不确定如何从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

1 个答案:

答案 0 :(得分:4)

您将euvar定义为dicts列表。但是您正试图直接访问第一个列表项。根据我的感觉,你只想要一个像这样的字典:

euvar: {dc1: "1.1.1.1", dc2: "1.2.2.2"}

对于那些喜欢可读性的人:

euvar:
  dc1: 1.1.1.1
  dc2: 1.2.2.2

然后您就可以{{ euvar.dc1 }}访问它了。

如果您真的打算定义一个dicts列表,那么您可以像@ᴳᵁᴵᴰᴼ已经对问题发表评论那样访问它:{{ euvar[0].dc1 }}

请同时查看Ansibles inventory groupsgroup_vars。变量的名称表明您正在使用Ansibles best practices