network/tests/playbooks/tests_network_state.yml
Rich Megginson 65e74567d4 refactor: support Ansible 2.19
The big problem was trying to use `vars` with `import_playbook`.

We do not need to use `import_playbook` when `include_tasks` will
work.  Perhaps the original author of these tests thought that
the play `roles` keyword was the only way to invoke roles, so
that had to be "called" using an `import_playbook`?

Use `include_tasks` instead of `import_playbook`, and move some
of those "tasks" playbooks to be tasks files in tests/tasks.

Use `include_role` instead of `import_role`.

Do not set variables using `set_fact` if they have already been
set at the appropriate scope using `vars`.

"Modernize" the code somewhat.

Improve formatting.

Work around an Ansible bug https://github.com/ansible/ansible/issues/85394

Fix ansible-lint and ansible-test issues related newer versions of
those tools.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2025-07-02 14:42:43 -06:00

198 lines
6.6 KiB
YAML

# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for configuring network using network state variable
hosts: all
vars:
type: veth
interface0: ethtest0
interface1: ethtest1
tasks:
- name: "Set type={{ type }} and interface={{ interface0 }}" # noqa name
set_fact:
type: "{{ type }}"
interface: "{{ interface0 }}"
- name: Include the task 'show_interfaces.yml'
include_tasks: tasks/show_interfaces.yml
- name: Include the task 'manage_test_interface.yml'
include_tasks: tasks/manage_test_interface.yml
vars:
state: present
- name: Include the task 'assert_device_present.yml'
include_tasks: tasks/assert_device_present.yml
- name: "Set type={{ type }} and interface={{ interface1 }}" # noqa name
set_fact:
type: "{{ type }}"
interface: "{{ interface1 }}"
- name: Include the task 'show_interfaces.yml' again
include_tasks: tasks/show_interfaces.yml
- name: Include the task 'manage_test_interface.yml' after changing interface
include_tasks: tasks/manage_test_interface.yml
vars:
state: present
- name: Include the task 'assert_device_present.yml' after changing interface
include_tasks: tasks/assert_device_present.yml
- name: Configure the IP addresses
import_role:
name: linux-system-roles.network
vars:
network_state:
interfaces:
- name: ethtest0
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.250
prefix-length: 24
dhcp: false
ipv6:
enabled: true
address:
- ip: 2001:db8::1:1
prefix-length: 64
autoconf: false
dhcp: false
- name: ethtest1
type: ethernet
state: up
ipv4:
enabled: true
auto-dns: false
address:
- ip: 192.168.122.88
prefix-length: 24
dhcp: false
ipv6:
enabled: true
auto-dns: false
dhcp: true
- name: Get the ethtest0 state configuration
command: nmstatectl show ethtest0
register: ethtest0_state
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Get the ethtest1 state configuration
command: nmstatectl show ethtest1
register: ethtest1_state
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Assert that the ethtest0 state configuration contains the specified
settings
assert:
that:
- ethtest0_state.stdout is search("192.168.122.250")
- ethtest0_state.stdout is search("2001:db8::1:1")
msg: the ethtest0 state configuration does not contain the specified
settings
- name: Assert that the ethtest1 state configuration contains the specified
settings
assert:
that:
- ethtest1_state.stdout is search("192.168.122.88")
msg: the ethtest1 state configuration does not contain the specified
settings
- name: Configure the route
import_role:
name: linux-system-roles.network
vars:
network_state:
routes:
config:
- destination: 192.0.2.100/30
metric: 150
next-hop-address: 192.168.122.250
next-hop-interface: ethtest0
table-id: 254
- name: Get the route configuration
command: nmstatectl show
register: route
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Assert that the route configuration contains the specified route
assert:
that:
- route.stdout is search("destination:(\s+)192.0.2.100/30")
- route.stdout is search("metric:(\s+)150")
- route.stdout is search("next-hop-address:(\s+)192.168.122.250")
- route.stdout is search("next-hop-interface:(\s+)ethtest0")
- route.stdout is search("table-id:(\s+)254")
msg: the route configuration does not contain the specified route
- name: Configure the DNS
import_role:
name: linux-system-roles.network
vars:
network_state:
dns-resolver:
config:
search:
- example.com
- example.org
server:
- 2001:4860:4860::8888
- name: Get the DNS configuration from nmstatectl
command: nmstatectl show
register: nmstatectl
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Get the DNS configuration from the file
`/run/NetworkManager/no-stub-resolv.conf`
command: cat /run/NetworkManager/no-stub-resolv.conf
register: no_stub_resolvconf
changed_when: false
- name: Assert that the nmstatectl contains the specified DNS configuration
assert:
that:
- nmstatectl.stdout is search("example.com")
- nmstatectl.stdout is search("example.org")
- nmstatectl.stdout is search("2001:4860:4860::8888")
msg: the nmstatectl does not contain the specified DNS configuration
- name: Assert that the file `/run/NetworkManager/no-stub-resolv.conf`
contains the specified DNS configuration
assert:
that:
- no_stub_resolvconf.stdout is search("example.com")
- no_stub_resolvconf.stdout is search("example.org")
- no_stub_resolvconf.stdout is search("2001:4860:4860::8888")
msg: the file `/run/NetworkManager/no-stub-resolv.conf` does not
contain the specified DNS configuration
- name: Purge the DNS
import_role:
name: linux-system-roles.network
vars:
network_state:
dns-resolver:
config: {}
- name: Include the task 'delete_interface.yml'
include_tasks: tasks/delete_interface.yml
- name: Include the task 'assert_device_absent.yml'
include_tasks: tasks/assert_device_absent.yml
- name: "Set interface={{ interface0 }}"
set_fact:
type: "{{ type }}"
interface: "{{ interface0 }}"
- name: Include the task 'delete_interface.yml' again
include_tasks: tasks/delete_interface.yml
- name: Include the task 'assert_device_absent.yml' again
include_tasks: tasks/assert_device_absent.yml
- name: Verify network state restored to default
include_tasks: tasks/check_network_dns.yml
...