使用Ansible中的when条件在循环中查找和使用第一个匹配项

时间:2019-02-12 01:38:02

标签: loops ansible juniper

因此,我正在尝试在Juniper ex2300上配置接口,但我只想配置第一个免费接口。我知道,当您注册值时,所有值都会注册,这很令人沮丧,因为我不需要跳过的值。

但是我只想使用满足任务Print results中条件的第一个界面,但是我一直想做的是:

- name: Configure Interface
  juniper_junos_command
    commands:
       set interface {{item}} unit 0 family inet address 192.168.1.1/24
  loop: "{{ready.results}}"
  when: 
    #item not skipped
    #end loop

Yaml文件

  tasks:
    - name: Get interfaces
      juniper_junos_command:
        formats: xml
        commands:
          - show interfaces
      register: inter

    - name: Print results
      debug:
        msg: "{{item.name}}"
      loop: "{{ inter.parsed_output['interface-information']['physical-interface'] }}"
      loop_control:
        label: "{{ item.name }}"
      when:
        - item['oper-status'] == "down"
        - item['admin-status'] == "down"
        - item.name | regex_search('(^[xg]e.*)')
        - item.description == "** SPARE **"
      ignore_errors: yes
      register: ready

这是ready输出的一部分
我不想使用的第一项,我要做的第二项。但是,无论是ge-0 / 0/0还是ge-0 / 0/1

"ready": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "ge-0/0/0",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": {
                    "active-alarms": {
                        "interface-alarms": {
                            "ethernet-alarm-link-down": ""
                        }
                    },
                    "active-defects": {
                        "interface-alarms": {
                            "ethernet-alarm-link-down": ""
                        }
                    },
                    "admin-status": "up",
                    "bpdu-error": "none",
                    "current-physical-address": "f0:4b:3a:72:48:63",
                    "description": "Link to CLIENTS",
                    "duplex": "Auto",
                    "hardware-physical-address": "f0:4b:3a:72:48:63",
                    "ieee-802-3az-eee": "disabled",
                    "if-auto-negotiation": "enabled",
                    "if-config-flags": {
                        "iff-hardware-down": "",
                        "iff-snmp-traps": "",
                        "internal-flags": "0x4000"
                    },
                    "if-device-flags": {
                        "ifdf-down": "",
                        "ifdf-present": "",
                        "ifdf-running": ""
                    },
                    "if-flow-control": "disabled",
                    "if-media-flags": {
                        "ifmf-none": ""
                    },
                    "if-media-type": "Copper",
                    "if-remote-fault": "online",
                    "interface-flapped": "2000-01-02 11:50:01 NZDT (02:48:34 ago)",
                    "interface-transmit-statistics": "Disabled",
                    "l2pt-error": "none",
                    "ld-pdu-error": "none",
                    "link-level-type": "Ethernet",
                    "link-mode": "Full-duplex",
                    "local-index": "648",
                    "logical-interface": {
                        "address-family": {
                            "address-family-flags": {
                                "ifff-is-primary": "",
                                "ifff-port-mode-trunk": "",
                                "internal-flags": "0x4400000"
                            },
                            "address-family-name": "eth-switch",
                            "mtu": "1514"
                        },
                        "encapsulation": "Ethernet-Bridge",
                        "filter-information": "",
                        "if-config-flags": {
                            "iff-device-down": "",
                            "iff-snmp-traps": "",
                            "internal-flags": "0x24024000"
                        },
                        "local-index": "561",
                        "logical-interface-bandwidth": "0",
                        "name": "ge-0/0/0.0",
                        "policer-overhead": "",
                        "snmp-index": "526",
                        "traffic-statistics": {
                            "input-packets": "1448",
                            "output-packets": "20"
                        }
                    },
                    "loopback": "disabled",
                    "mtu": "1514",
                    "name": "ge-0/0/0",
                    "oper-status": "down",
                    "physical-interface-cos-information": {
                        "physical-interface-cos-hw-max-queues": "8",
                        "physical-interface-cos-use-max-queues": "8"
                    },
                    "snmp-index": "513",
                    "sonet-mode": "LAN-PHY",
                    "source-filtering": "disabled",
                    "speed": "Auto",
                    "traffic-statistics": {
                        "input-bps": "0",
                        "input-pps": "0",
                        "output-bps": "0",
                        "output-pps": "0"
                    }
                },
                "msg": "ge-0/0/0"
            },{
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "ge-0/0/1",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": {
                    "active-alarms": {
                        "interface-alarms": {
                            "ethernet-alarm-link-down": ""
                        }
                    },
                    "active-defects": {
                        "interface-alarms": {
                            "ethernet-alarm-link-down": ""
                        }
                    },
                    "admin-status": "up",
                    "bpdu-error": "none",
                    "current-physical-address": "f0:4b:3a:72:48:64",
                    "description": "Link to CLIENTS",
                    "duplex": "Auto",
                    "hardware-physical-address": "f0:4b:3a:72:48:64",
                    "ieee-802-3az-eee": "disabled",
                    "if-auto-negotiation": "enabled",
                    "if-config-flags": {
                        "iff-hardware-down": "",
                        "iff-snmp-traps": "",
                        "internal-flags": "0x4000"
                    },
                    "if-device-flags": {
                        "ifdf-down": "",
                        "ifdf-present": "",
                        "ifdf-running": ""
                    },
                    "if-flow-control": "disabled",
                    "if-media-flags": {
                        "ifmf-none": ""
                    },
                    "if-media-type": "Copper",
                    "if-remote-fault": "online",
                    "interface-flapped": "2000-01-02 11:41:10 NZDT (02:57:26 ago)",
                    "interface-transmit-statistics": "Disabled",
                    "l2pt-error": "none",
                    "ld-pdu-error": "none",
                    "link-level-type": "Ethernet",
                    "link-mode": "Half-duplex",
                    "local-index": "649",
                    "logical-interface": {
                        "address-family": {
                            "address-family-flags": {
                                "ifff-port-mode-trunk": "",
                                "internal-flags": "0x4400000"
                            },
                            "address-family-name": "eth-switch",
                            "mtu": "1514"
                        },
                        "encapsulation": "Ethernet-Bridge",
                        "filter-information": "",
                        "if-config-flags": {
                            "iff-device-down": "",
                            "iff-snmp-traps": "",
                            "internal-flags": "0x24024000"
                        },
                        "local-index": "562",
                        "name": "ge-0/0/1.0",
                        "policer-overhead": "",
                        "snmp-index": "531",
                        "traffic-statistics": {
                            "input-packets": "0",
                            "output-packets": "0"
                        }
                    },
                    "loopback": "disabled",
                    "mtu": "1514",
                    "name": "ge-0/0/1",
                    "oper-status": "down",
                    "physical-interface-cos-information": {
                        "physical-interface-cos-hw-max-queues": "8",
                        "physical-interface-cos-use-max-queues": "8"
                    },
                    "snmp-index": "514",
                    "sonet-mode": "LAN-PHY",
                    "source-filtering": "disabled",
                    "speed": "Auto",
                    "traffic-statistics": {
                        "input-bps": "0",
                        "input-pps": "0",
                        "output-bps": "0",
                        "output-pps": "0"
                    }
                },
                "msg": "ge-0/0/1"
            }        
        ]
    }
}

我该如何使用我在命令中遇到的第一项然后停止循环?

0 个答案:

没有答案