diff --git a/tests/tasks/assert-device_absent.yml b/tests/tasks/assert-device_absent.yml index eee79d4..67b83ad 100644 --- a/tests/tasks/assert-device_absent.yml +++ b/tests/tasks/assert-device_absent.yml @@ -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" diff --git a/tests/tasks/assert-device_present.yml b/tests/tasks/assert-device_present.yml index 1f72fb6..e0d4097 100644 --- a/tests/tasks/assert-device_present.yml +++ b/tests/tasks/assert-device_present.yml @@ -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" diff --git a/tests/tasks/get-current_interfaces.yml b/tests/tasks/get-current_interfaces.yml new file mode 100644 index 0000000..33a4a76 --- /dev/null +++ b/tests/tasks/get-current_interfaces.yml @@ -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 }}" diff --git a/tests/tasks/get-interface_stat.yml b/tests/tasks/get-interface_stat.yml new file mode 100644 index 0000000..a8b8e5b --- /dev/null +++ b/tests/tasks/get-interface_stat.yml @@ -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 diff --git a/tests/tasks/manage-test-interface.yml b/tests/tasks/manage-test-interface.yml index 6d6add8..8709e3a 100644 --- a/tests/tasks/manage-test-interface.yml +++ b/tests/tasks/manage-test-interface.yml @@ -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" diff --git a/tests/tasks/show-interfaces.yml b/tests/tasks/show-interfaces.yml index a4b8c44..704e8c5 100644 --- a/tests/tasks/show-interfaces.yml +++ b/tests/tasks/show-interfaces.yml @@ -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 }}"