mirror of
https://github.com/linux-system-roles/network.git
synced 2026-07-25 11:54:06 +00:00
tests: Do not use ansible_interfaces
ansible_interfaces requires iproute to be present which might not be the case for container images. Check /sys/class/net to avoid the dependency.
This commit is contained in:
parent
7b503a3862
commit
5273f55166
6 changed files with 42 additions and 22 deletions
|
|
@ -1,9 +1,7 @@
|
|||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
---
|
||||
# need to run setup again when this is just a task to ensure ansible_interfaces
|
||||
# is current
|
||||
- setup:
|
||||
- include: get-interface_stat.yml
|
||||
- name: "assert that interface {{ interface }} is absent"
|
||||
assert:
|
||||
that: "{{ not interface in ansible_interfaces }}"
|
||||
msg: "{{ interface }} is in ansible_interfaces: {{ ansible_interfaces }}"
|
||||
that: not interface_stat.stat.exists
|
||||
msg: "{{ interface }} exists"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
---
|
||||
# need to run setup again when this is just a task to ensure ansible_interfaces
|
||||
# is current
|
||||
- setup:
|
||||
- include: get-interface_stat.yml
|
||||
- name: "assert that interface {{ interface }} is present"
|
||||
assert:
|
||||
that: "{{ interface in ansible_interfaces }}"
|
||||
msg: "{{ interface }} is not in ansible_interfaces: {{ ansible_interfaces }}"
|
||||
that: interface_stat.stat.exists
|
||||
msg: "{{ interface }} does not exist"
|
||||
|
|
|
|||
8
tests/tasks/get-current_interfaces.yml
Normal file
8
tests/tasks/get-current_interfaces.yml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
---
|
||||
- command: ls -1
|
||||
args:
|
||||
chdir: /sys/class/net
|
||||
register: _current_interfaces
|
||||
- set_fact:
|
||||
current_interfaces: "{{ _current_interfaces.stdout_lines }}"
|
||||
9
tests/tasks/get-interface_stat.yml
Normal file
9
tests/tasks/get-interface_stat.yml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
---
|
||||
- name: "Get stat for interface {{ interface }}"
|
||||
stat:
|
||||
get_attributes: false
|
||||
get_checksum: false
|
||||
get_mime: false
|
||||
path: "/sys/class/net/{{ interface }}"
|
||||
register: interface_stat
|
||||
|
|
@ -8,32 +8,38 @@
|
|||
msg: "type needs to be dummy, tap or veth, not '{{ type }}'"
|
||||
when: type not in ["dummy", "tap", "veth"]
|
||||
|
||||
# run setup again to make sure ansible_interfaces is accurate
|
||||
- setup:
|
||||
# - include: get-current_interfaces.yml
|
||||
- include: show-interfaces.yml
|
||||
|
||||
# veth
|
||||
# veth
|
||||
- name: Create veth interface {{ interface }}
|
||||
shell: ip link add {{ interface }} type veth peer name peer{{ interface }}
|
||||
when: "type == 'veth' and state == 'present' and interface not in ansible_interfaces"
|
||||
when: "type == 'veth' and state == 'present' and
|
||||
interface not in current_interfaces"
|
||||
|
||||
- name: Delete veth interface {{ interface }}
|
||||
shell: ip link del {{ interface }} type veth
|
||||
when: "type == 'veth' and state == 'absent' and interface in ansible_interfaces"
|
||||
when: "type == 'veth' and state == 'absent' and
|
||||
interface in current_interfaces"
|
||||
|
||||
# dummy
|
||||
# dummy
|
||||
- name: Create dummy interface {{ interface }}
|
||||
shell: ip link add "{{ interface }}" type dummy
|
||||
when: "type == 'dummy' and state == 'present' and interface not in ansible_interfaces"
|
||||
when: "type == 'dummy' and state == 'present' and
|
||||
interface not in current_interfaces"
|
||||
|
||||
- name: Delete dummy interface {{ interface }}
|
||||
shell: ip link del "{{ interface }}" type dummy
|
||||
when: "type == 'dummy' and state == 'absent' and interface in ansible_interfaces"
|
||||
when: "type == 'dummy' and state == 'absent' and
|
||||
interface in current_interfaces"
|
||||
|
||||
# tap
|
||||
# tap
|
||||
- name: Create tap interface {{ interface }}
|
||||
shell: ip tuntap add dev {{ interface }} mode tap
|
||||
when: "type == 'tap' and state == 'present' and interface not in ansible_interfaces"
|
||||
when: "type == 'tap' and state == 'present'
|
||||
and interface not in current_interfaces"
|
||||
|
||||
- name: Delete tap interface {{ interface }}
|
||||
shell: ip tuntap del dev {{ interface }} mode tap
|
||||
when: "type == 'tap' and state == 'absent' and interface in ansible_interfaces"
|
||||
when: "type == 'tap' and state == 'absent' and
|
||||
interface in current_interfaces"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
---
|
||||
- include: get-current_interfaces.yml
|
||||
- debug:
|
||||
msg: "ansible_interfaces: {{ ansible_interfaces }}"
|
||||
msg: "current_interfaces: {{ current_interfaces }}"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue