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

Mic*_*and 0 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
Run Code Online (Sandbox Code Playgroud)

我有一个类似的安塞任务

- 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
Run Code Online (Sandbox Code Playgroud)

这始终失败:

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

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

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

小智 5

就像Konstaintin所说:它将network_interface_scripts视为字符串文字'network_interface_scripts'.

而是尝试:

with_subelements:
- "{{ network_interface_scripts | default([]) }}"
- "{{ lifecycle_entries }}"
Run Code Online (Sandbox Code Playgroud)