如何使用Ansible模块ios_l2_interfaces遍历ios事实?

时间:2020-01-24 11:29:02

标签: ansible cisco-ios

如果描述与特定字词匹配(-代表接入点),我想遍历Cisco IOS交换机上的每个接口并设置特定的第2层配置。

这是剧本:

---
- name: Cisco Layer 2 Interface
  hosts: test
  strategy: free
  vars:
   ansible_command_timeout: 60
   desc_search: "-ap-"
  tasks:
   - name: IOS facts
     ios_facts:
      gather_subset:
       - '!all'
       - '!min'
      gather_network_resources:
       - 'interfaces'
   - name: Layer 2 config
     ios_l2_interfaces:
      config:
       - name: "{{ item.key }}"
         trunk:
          allowed_vlans: 10,20,30,40
          native_vlan: 10
          encapsulation: dot1q
      state: replaced
     loop: "{{ ansible_net_interfaces|dict2items }}"
     when: item.value.description is search(desc_search)
...

了解事实是可以的。但是,模块 ios_l2_interfaces 失败,并显示以下错误。似乎-名称:item.key 的键不起作用,但是错误消息清楚地表明它在那里。

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'get'
failed: [demo-sw-02.nb.edeka.net] (item={'key': u'GigabitEthernet1/0/24', 'value': {u'macaddress': u'xxxx.xxxx.xxxx', u'lineprotocol': None, u'description': u'#demo-ap-02', u'duplex': None, u'mediatype': u'10/100/1000BaseTX', u'mtu': 1500, u'operstatus': u'up', u'bandwidth': 1000000, u'ipv4': [], u'type': u'Gigabit Ethernet'}}) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": {
        "key": "GigabitEthernet1/0/24",
        "value": {
            "bandwidth": 1000000,
            "description": "#demo-ap-02",
            "duplex": null,
            "ipv4": [],
            "lineprotocol": null,
            "macaddress": "xxxx.xxxx.xxxx",
            "mediatype": "10/100/1000BaseTX",
            "mtu": 1500,
            "operstatus": "up",
            "type": "Gigabit Ethernet"
        }
    },
    "rc": 1
}

MSG:

MODULE FAILURE
See stdout/stderr for the exact error


MODULE_STDERR:

Traceback (most recent call last):
  File "/home/ansible/.ansible/tmp/ansible-local-7161cuIEBE/ansible-tmp-1579863214.5-251145517474981/AnsiballZ_ios_l2_interfaces.py", line 102, in <module>
    _ansiballz_main()
  File "/home/ansible/.ansible/tmp/ansible-local-7161cuIEBE/ansible-tmp-1579863214.5-251145517474981/AnsiballZ_ios_l2_interfaces.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/ansible/.ansible/tmp/ansible-local-7161cuIEBE/ansible-tmp-1579863214.5-251145517474981/AnsiballZ_ios_l2_interfaces.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible.modules.network.ios.ios_l2_interfaces', init_globals=None, run_name='__main__', alter_sys=True)
  File "/usr/lib64/python2.7/runpy.py", line 188, in run_module
    fname, loader, pkg_name)
  File "/usr/lib64/python2.7/runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/modules/network/ios/ios_l2_interfaces.py", line 364, in <module>
  File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/modules/network/ios/ios_l2_interfaces.py", line 359, in main
  File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 63, in execute_module
  File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 88, in set_config
  File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 113, in set_state
  File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 134, in _state_replaced
  File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/utils/utils.py", line 75, in filter_dict_having_none_value
AttributeError: 'NoneType' object has no attribute 'get'

我正在使用ansible 2.9.4。

1 个答案:

答案 0 :(得分:0)

“何时”条件正常工作。 “ filter_dict_having_none_value”表明dict2items过滤器存在问题。