Ansible AnsibleUndefinedVariable没有属性

问题描述 投票:2回答:1

我有以下文件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
yaml ansible
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.