Ansible,YAML和Syntax

时间:2015-03-12 23:02:33

标签: python networking yaml ansible ansible-playbook

我正在尝试创建一个Ansible配置,该配置将运行一个playbook并利用单个变量文件来创建具有多个项目的单个配置。我正在尝试以下语法,它失败了。我该如何解决这个问题?

乏/ main.yml

---
or1host1:

      - interface: 1/1
        description: or1-servertest
        TRUNK: true
        allowedVlans: 101-103
        NVLAN: true
        nativeVLAN: 101
        ACCESS: false
        accessVlan: none
        PC: true
        pcNUM: 10

      - interface: 1/2
        description: or1-servertest2
        TRUNK: false
        allowedVlans: 101-103
        NVLAN: false
        nativeVLAN: 101
        ACCESS: true
        accessVlan: none
        PC: true
        pcNUM: 10

模板/ nxos.j2

{% for interface in or1host1 %}
interface Ethernet{{item.interface}}
description {{item.description}}
{% if item.TRUNK %}
  switchport mode trunk
  switchport trunk allowed vlan {{item.allowedVlans}}
  spanning-tree port type edge trunk
{% if item.NVLAN %}
  switchport trunk native vlan {{item.nativeVLAN}}
{% endif %}
{% endif %}
{% if item.ACCESS %}
  switchport mode access
  switchport access vlan {{item.accessVlan}}
  spanning-tree port type edge
{% endif %}
{% if item.PC %}
  channel-group {{item.pcNUM}} mode active
{% endif %}
  no shut
{% endfor %}

运行Playbook时收到以下错误。

PLAY [Generate Configuration Files] ******************************************* 

GATHERING FACTS *************************************************************** 
ok: [localhost]

TASK: [nxos | Generate configuration files] *********************************** 
fatal: [localhost] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'str object' has no attribute 'interface'", 'failed': True}
fatal: [localhost] => {'msg': 'One or more items failed.', 'failed': True, 'changed': False, 'results': [{'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'str object' has no attribute 'interface'", 'failed': True}]}

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/gituser/site.retry

localhost                  : ok=1    changed=0    unreachable=1    failed=

0

2 个答案:

答案 0 :(得分:1)

最后想出来了,见下面的例子。

<强>〜/瓦尔/ mail.yml

---
nxos:
- {hostname: testhost}
interfaces:
- {ACCESS: 'true', NVLAN: 'true', PC: 'true', TRUNK: 'true', accessVlan: '1', allowedVlans: 500-600,
  desC: serverhost-1005, intF: eth1/1, nativeVLAN: '56', pcNUM: '23'}
- {ACCESS: 'true', NVLAN: 'true', PC: 'true', TRUNK: 'true', accessVlan: '1', allowedVlans: 500-600,
  desC: serverhost-1006, intF: eth1/2, nativeVLAN: '56', pcNUM: '23'}

<强>〜/模板/ nxos.j2

HOST: {{item.hostname}}

-----------------------------------------------------------
-----------------------------------------------------------

{%for i in interfaces %}
interface Ethernet{{i.intF}}
  description {{i.desC}}
{% if i.TRUNK %}
  switchport mode trunk
  switchport trunk allowed vlan {{i.allowedVlans}}
  spanning-tree port type edge trunk
{% if i.NVLAN %}
  switchport trunk native vlan {{i.nativeVLAN}}
{% endif %}
{% endif %}
{% if i.ACCESS %}
  switchport mode access
  switchport access vlan {{i.accessVlan}}
  spanning-tree port type edge
{% endif %}
{% if i.PC %}
  channel-group {{i.pcNUM}} mode active
{% endif %}
  no shut
!
{% endfor %}

<强>配置

HOST: testhost

-----------------------------------------------------------
-----------------------------------------------------------

interface Etherneteth1/1
  description serverhost-1005
  switchport mode trunk
  switchport trunk allowed vlan 500-600
  spanning-tree port type edge trunk
  switchport trunk native vlan 56
  switchport mode access
  switchport access vlan 1
  spanning-tree port type edge
  channel-group 23 mode active
  no shut
!
interface Etherneteth1/2
  description serverhost-1006
  switchport mode trunk
  switchport trunk allowed vlan 500-600
  spanning-tree port type edge trunk
  switchport trunk native vlan 56
  switchport mode access
  switchport access vlan 1
  spanning-tree port type edge
  channel-group 23 mode active
  no shut
!

答案 1 :(得分:0)

这里有一些事情。首先,当你不处于JSON模式时,你真的需要注意缩进。当您使用4个项目缩进列表项时,所有其他列表项都需要具有相同的空格数。你的第二个项目有6个空格,这将是一个语法错误,如果没有完全松散的元素interface: 1/2,这是这里的主要问题,因为它没有意义,并且没有办法如何可以解析为数据结构。在列表中,您可以拥有任意数量的列表项,但在同一级别上没有松散的属性。我想你的意思是把它放在第二个列表项中。两次修正后看起来像这样:

---
or1-host1:
  - { interface: 1/1, 
      description: or1-servertest, 
      TRUNK: true, allowedVlans: 101-103, 
      NVLAN: true, nativeVLAN: 101, 
      ACCESS: false, accessVlan: none, 
      PC: true, pcNUM: 10
    }

  - {  interface: 1/2,
       description: or1-servertest2, 
       TRUNK: false, allowedVlans: 101-103, 
       NVLAN: false, nativeVLAN: 101, 
       ACCESS: true, accessVlan: none, 
       PC: true, pcNUM: 10
    }

虽然JSON是YML的有效子集,但我发现严格遵守YML语法会更具可读性,如下所示:

--- 
or1-host1:
  - interface: 1/1
    description: or1-servertest
    TRUNK: true
    allowedVlans: 101-103
    NVLAN: true
    nativeVLAN: 101
    ACCESS: false
    accessVlan: none
    PC: true
    pcNUM: 10

  - interface: 1/2 
    description: or1-servertest2
    TRUNK: false
    allowedVlans: 101-103
    NVLAN: false
    nativeVLAN: 101
    ACCESS: true
    accessVlan: none 
    PC: true
    pcNUM: 10
...

在您的模板中,虽然您无法像这样访问1/11/2。也许你想循环模板中的接口?我不确定。

{% for interface in or1-host1 %}
  ...
{% endfor %}

我认为您必须重命名or1-host1,因为连字符or1_host1无效,因此它看起来不像有效的变量名。