0

我正在从瞻博网络设备中提取事实并将该信息推送到 NetBox。

下面是我的剧本,基本上 set_fact 给了我最后一个结果,然后 netbox play 将相同的结果发送到我的所有接口,这不是我想要的。

- name: SETTING INT TYPE VIRTUAL
  set_fact:
    interfacetype: "Virtual"
  loop: "{{ansible_network_resources.interfaces}}"
  when: item.name == "irb" or 
        item.name == "lo0" 
      
- name: SETTING INT TYPE 1GE
  set_fact:
    interfacetype: "1000BASE-T (1GE)"
  loop: "{{ansible_network_resources.interfaces}}"
  when: item.name | regex_search('(ge-)')
  register: inter1

- name: SETTING INT TYPE 40GE
  set_fact:
    interfacetype: "QSFP+ (40GE)"
  loop: "{{ansible_network_resources.interfaces}}"
  when: item.name | regex_search('(et-)') and
        ansible_net_model | regex_search('^ex[2-4][2346]00|qfx5100$')

- name: CREATE DEVICE INTERFACES
  netbox_device_interface:
    netbox_url: https://netbox.something.org
    netbox_token: 99999999999999999999999999999999999
    data:
      device: "{{ ansible_net_hostname }}"
      name: "{{ item.name }}"
      description: "{{ item.description|default('') }}"
      enabled: "{{ item.enabled }}"
      mtu: "{{ item.mtu|default('') }}"
      type: "{{ interfacetype }}"
    state: present
  loop: "{{ansible_network_resources.interfaces}}"
  delegate_to: localhost

我认为基于上述内容,以下内容更有意义。我想我需要在列表中组合每个部分,每个部分都有一个指向 ge 的字典 - 一个类似于intertype:"1000BASE-T (1GE)"的新项目

这样我就可以在最后一场比赛中使用该词典列表。

这就是我得到的:

ok: [device1.something.org] => {
"ansible_network_resources.interfaces": [
    {
        "enabled": true,
        "mtu": 9216,
        "name": "ge-0/0/0"
    },
    {
        "enabled": false,
        "mtu": 9216,
        "name": "ge-0/0/1"
    },
    {
        "enabled": false,
        "mtu": 9216,
        "name": "ge-0/0/2"
    },
    {
        "enabled": false,
        "mtu": 9216,
        "name": "ge-0/0/3"
    },
    {
        "enabled": false,
        "mtu": 9216,
        "name": "ge-0/0/4"
    },
    {
        "description": "agaga2",
        "enabled": true,
        "mtu": 9216,
        "name": "et-0/1/0"
    },
    {
        "enabled": false,
        "mtu": 9216,
        "name": "et-0/1/1"
    },
    {
        "enabled": true,
        "mtu": 9216,
        "name": "xe-0/2/0"
    },
    {
        "enabled": false,
        "mtu": 9216,
        "name": "xe-0/2/1"
    },
    {
        "enabled": false,
        "mtu": 9216,
        "name": "xe-0/2/2"
    },
    {
        "description": "blabla1",
        "enabled": true,
        "mtu": 9216,
        "name": "xe-0/2/3"
    },
    {
        "enabled": true,
        "name": "irb"
    },
    {
        "enabled": true,
        "name": "lo0"
    }
]

}

我可以在里面添加一个新字典,结果如下:

    {
    "intertype":"Virtual"  
    "enabled": true,
        "name": "lo0"
    }
    {
    "intertype":"QSFP+ (40GE)"  
    "enabled": false,
    "mtu": 9216,
     "name": "et-0/2/2"
    },
    {
    "intertype":"1000BASE-T (1GE)"     
    "enabled": false,
        "mtu": 9216,
        "name": "ge-0/0/1"
    },
4

1 回答 1

0

在您的代码中,变量interfacetype在每次迭代中设置

- name: SETTING INT TYPE VIRTUAL
  set_fact:
    interfacetype: "Virtual"
  loop: "{{ansible_network_resources.interfaces}}"
  when: item.name == "irb" or 
        item.name == "lo0" 

我将此任务的逻辑读为“如果 [irb, lo0] 中的任何一个在将 interfacetype 设置为 Virtual 的接口中”。选择接口并与列表相交会更有效。例如,给定数据

    ansible_network_resources:
      interfaces:
        - {enabled: true, name: em1, mtu: '1476'}
        - {enabled: true, name: irb, mtu: '1514'}
        - {enabled: true, name: lo0, mtu: "Unlimited"}
    sel_intfs: [irb, lo0]

选择接口

    - set_fact:
        my_intfs: "{{ ansible_network_resources.interfaces|
                      map(attribute='name')|list }}"

  my_intfs:
  - em1
  - irb
  - lo0

和相交

    - set_fact:
        no_intfs: "{{ sel_intfs|intersect(my_intfs)|length }}"

给出常用接口的数量

  no_intfs: '2'

当你把它放在一起

    - set_fact:
        interfacetype: Virtual
      when: no_intfs|int > 0
      vars:
        my_intfs: "{{ ansible_network_resources.interfaces|
                              map(attribute='name')|list }}"
        no_intfs: "{{ sel_intfs|intersect(my_intfs)|length }}"
    - debug:
        var: interfacetype

  interfacetype: Virtual

在 intersect 为空列表的情况下,即没有公共接口,例如,sel_intfs: [irc, lo1]相同的任务给出

  interfacetype: VARIABLE IS NOT DEFINED!
于 2021-01-28T07:13:49.997 回答