Ansible"子元素查找需要一个字典"

时间:2016-12-08 18:01:50

标签: dictionary ansible

我的智慧结束了这一点。我的YAML看起来像这样:

network_interface_scripts:
  -
    interface: eth0
    lifecycle_entries:
      -
        commands:
          - "route add -net 172.17.2.0 netmask 255.255.255.0 gw 172.17.70.1 eth0"
          - "route add -net 10.102.0.0 netmask 255.255.0.0 gw 172.17.70.1 eth0"
        script_name: interface_eth0_up
        trigger_on: post-up
      -
        commands:
          - "route delete -net 172.17.2.0 netmask 255.255.255.0 gw 172.17.70.1 eth0"
          - "route delete -net 10.102.0.0 netmask 255.255.0.0 gw 172.17.70.1 eth0"
        script_name: interface_eth0_down
        trigger_on: pre-down

我有一个类似

的安全任务
- name: Check that trigger_on has valid value
  fail: msg="trigger_on has invalid value. Allowed values are pre-up, up, post-up, pre-down, down and post-down"
  when: item.1.trigger_on != "pre-up" and item.1.trigger_on != "up" and item.1.trigger_on != "post-up" and item.1.trigger_on != "pre-down" and item.1.trigger_on != "down" and item.1.trigger_on != "post-down"
  with_subelements:
    - network_interface_scripts | default([])
    - lifecycle_entries

这总是失败:

fatal: [fe1.prod.somedomain.de]: FAILED! => {"failed": true, "msg": "subelements lookup expects a dictionary, got 'network_interface_scripts | default([])'"}

我尝试了一个字典的在线验证器,它说它是有效的YAML。我错过了什么?为什么ansible不承认这是一本字典?

编辑:Ansible版本是2.2.0.0,搜索没有找到任何有用的东西... sry如果之前已经被问到

1 个答案:

答案 0 :(得分:5)

像Konstaintin所说: 它将ne​​twork_interface_scripts视为字符串文字'network_interface_scripts'。

而是尝试:

with_subelements:
- "{{ network_interface_scripts | default([]) }}"
- "{{ lifecycle_entries }}"