在没有现有值的情况下循环

  • 本文关键字:情况下 循环 ansible
  • 更新时间 :
  • 英文 :


我正在从cisco路由器收集事实,然后我想在netbox中创建ip地址对象,如下所示:

- name: gather facts
cisco.ios.ios_facts:
gather_subset: all
register: result
- name: Add Interface IP address to Netbox
netbox_ip_address:
netbox_url: xxx
netbox_token: xxx
data:
address: "{{ item.value.ipv4.0.address}}/{{ item.value.ipv4.0.subnet}}"
assigned_object:
name:  "{{ item.key }}"
device: "{{ ansible_net_hostname }}"
state: present
with_dict: "{{ ansible_net_interfaces }}"
ignore_errors: yes

所以我循环一个dict变量-";ansible_net_interfaces"-它看起来像这样:

"ansible_net_interfaces": {
"GigabitEthernet1": {
"bandwidth": 1000000,
"description": "dgfh",
"duplex": "Full",
"ipv4": [
{
"address": "10.0.0.1",
"subnet": "24"
},
{
"address": "10.10.20.48",
"subnet": "24"
}
],
"lineprotocol": "up",
"macaddress": "0050.56bf.9379",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "up",
"type": "CSR vNIC"
},
"Port-channel1": {
"bandwidth": 1000000,
"description": "CCIE-TEST",
"duplex": null,
"ipv4": [],
"lineprotocol": "down",
"macaddress": "001e.bd55.92c0",
"mediatype": null,
"mtu": 1501,
"operstatus": "down",
"type": "GEChannel"
},

当有一个没有IP地址的接口时,我有一个问题:

fatal: [xxx]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 0nnThe error appears to be in '/etc/ansible/playbooks/ios_facts.yml': line 37, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn    - name: Add Interface IP address to Netboxn      ^ heren"}

我如何更改该行:

address: "{{ item.value.ipv4.0.address}}/{{ item.value.ipv4.0.subnet}}"

所以如果item.value.ipv4.0.address不存在就不会有错误?

与_subelements一起使用。这将自动省略空的子元素列表,例如

- debug:
msg: |
name: {{ item.0.key }}
address: {{ item.1.address }}/{{ item.1.subnet }}
with_subelements:
- "{{ ansible_net_interfaces|dict2items }}"
- value.ipv4

给出(节略(

msg: |-
name: GigabitEthernet1
address: 10.0.0.1/24
msg: |-
name: GigabitEthernet1
address: 10.10.20.48/24

如果您只想使用列表中的第一个地址,请使用loop_control(在对问题的评论中,您会说:"它不适用于两个地址"(

- debug:
msg: |
name: {{ item.0.key }}
address: {{ item.1.address }}/{{ item.1.subnet }}
with_subelements:
- "{{ ansible_net_interfaces|dict2items }}"
- value.ipv4
loop_control:
extended: true
when: ansible_loop.index == 1

给出(节略(

ok: ...
msg: |-
name: GigabitEthernet1
address: 10.0.0.1/24
skipping: ...

您可以使用dict2itemsselectattritems2dict:删除没有IP地址的字典项

with_dict: "{{ ansible_net_interfaces | dict2items | selectattr('value.ipv4') | items2dict }}"

示例:

- hosts: localhost
connection: local
gather_facts: no
vars:
mydict:
"GigabitEthernet1":
"bandwidth": 1000000
"description": "dgfh"
"duplex": "Full"
"ipv4":
- "address": "10.0.0.1"
"subnet": "24"
- "address": "10.10.20.48"
"subnet": "24"
"lineprotocol": "up"
"macaddress": "0050.56bf.9379"
"mediatype": "Virtual"
"mtu": 1500
"operstatus": "up"
"type": "CSR vNIC"
"Port-channel1":
"bandwidth": 1000000
"description": "CCIE-TEST"
"duplex": null
"ipv4": []
"lineprotocol": "down"
"macaddress": "001e.bd55.92c0"
"mediatype": null
"mtu": 1501
"operstatus": "down"
"type": "GEChannel"
mydictwithip: "{{ mydict | dict2items | selectattr('value.ipv4') | items2dict }}"
tasks:
- debug:
var: mydictwithip

您的回答非常有帮助!我是这样做的:

- name: Add Interface IP address to Netbox
netbox_ip_address:
netbox_url: xxx
netbox_token: xxx
data:
address: "{{ item.value.ipv4.0.address}}/{{ item.value.ipv4.0.subnet}}"
assigned_object:
name:  "{{ item.key }}"
device: "{{ ansible_net_hostname }}"
state: present
with_dict: "{{ ansible_net_interfaces }}"
ignore_errors: yes
when: item.value.ipv4.0.address is defined

虽然我很好奇"ipv4":[]是否真的是未定义的元素,但它还是有效的?

skipping: [sandbox-iosxe-recomm-1.cisco.com] => (item={'key': 'Loopback21', 'value': {'description': None, 'macaddress': None, 'mtu': 1514, 'bandwidth': 8000000, 'mediatype': None, 'duplex': None, 'lineprotocol': 'up', 'operstatus': 'up', 'type': None, 'ipv4': []}})

我还想知道,如果接口上有两个或多个定义的ip地址,是否有一种在IPv4上循环的方法。例如,像这样的dict:

"ansible_net_interfaces": {
"GigabitEthernet1": {
"bandwidth": 1000000,
"description": "Configured with Netconf",
"duplex": "Full",
"ipv4": [
{
"address": "10.0.0.1",
"subnet": "24"
},
{
"address": "10.11.11.11",
"subnet": "24"
},
{
"address": "10.10.20.48",
"subnet": "24"
}
],
"lineprotocol": "up",
"macaddress": "0050.56bf.9379",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "up",
"type": "CSR vNIC"
},
"GigabitEthernet1.10": {
"bandwidth": 1000000,
"description": null,
"duplex": null,
"ipv4": [
{
"address": "1.1.1.1",
"subnet": "24"
}
],
"lineprotocol": "up",
"macaddress": "0050.56bf.9379",
"mediatype": null,
"mtu": 1500,
"operstatus": "up",
"type": "CSR vNIC"
},
"GigabitEthernet2": {
"bandwidth": 1000000,
"description": "Configured by RESTCONF Demo 2",
"duplex": "Full",
"ipv4": [],
"lineprotocol": "down",
"macaddress": "0050.56bf.ea76",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "down",
"type": "CSR vNIC"
},

最新更新