network/tests/tasks/find+remove_profile.yml
Rich Megginson 88a2609327 test: ensure role gathers the facts it uses by having test clear_facts before include_role
The role gathers the facts it uses.  For example, if the user uses
`ANSIBLE_GATHERING=explicit`, the role uses the `setup` module with the
facts and subsets it requires.

This change allows us to test this.  Before every role invocation, the test
will use `meta: clear_facts` so that the role starts with no facts.

Create a task file tests/tasks/run_role_with_clear_facts.yml to do the tasks
to clear the facts and run the role.  Note that this means we don't need to
use `gather_facts` for the tests.

Some vars defined using `ansible_facts` have been changed to be defined with
`set_fact` instead.  This is because of the fact that `vars` are lazily
evaluated - the var might be referenced when the facts have been cleared, and
will issue an error like `ansible_facts["distribution"] is undefined`.  This is
typically done for blocks that have a `when` condition that uses `ansible_facts`
and the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the `when` condition using `set_fact`.  This
is because the `when` condition is evaluated every time a task is invoked in the
block, and if the facts are cleared, this will raise an undefined variable error.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2026-03-19 10:47:09 -06:00

68 lines
2.1 KiB
YAML

# SPDX-License-Identifier: BSD-3-Clause
---
- name: Get connection profile for '{{ interface }}'
shell:
executable: /bin/bash
cmd: |
set -euo pipefail
connection="$(nmcli -g GENERAL.CONNECTION device show {{ interface | quote }})" || :
if [ -z "$connection" ]; then
nmcli device connect {{ interface | quote }} 1>&2
fi
nmcli -g GENERAL.CONNECTION device show {{ interface | quote }}
register: connection_name
changed_when: false
- name: Debug
shell: |
set -x
exec 1>&2
systemctl status NetworkManager || :
nmcli || :
nmcli device status || :
nmcli device show || :
nmcli connection show || :
nmcli connection show '{{ interface }}' || :
ip a
echo connection_name: {{ connection_name | to_nice_json | quote }} || :
ls -alrtF /etc/sysconfig/network-scripts || :
for file in /etc/sysconfig/network-scripts/ifcfg-*; do
if [ -f "$file" ]; then
echo "file: $file"
cat "$file" || :
fi
done
cat /etc/NetworkManager/NetworkManager.conf || :
find /etc/NetworkManager -type f -ls || :
for file in /etc/NetworkManager/system-connections/*.nmconnection; do
if [ -f "$file" ]; then
echo "file: $file"
cat "$file" || :
fi
done
for file in /etc/NetworkManager/conf.d/*; do
if [ -f "$file" ]; then
echo "file: $file"
cat "$file" || :
fi
done
find /usr/lib/NetworkManager -type f -ls || :
for file in /usr/lib/NetworkManager/conf.d/*; do
if [ -f "$file" ]; then
echo "file: $file"
cat "$file" || :
fi
done
NetworkManager --print-config
journalctl -u NetworkManager || :
changed_when: false
when: connection_name is failed or connection_name.stdout | length == 0
failed_when: connection_name is failed or connection_name.stdout | length == 0
- name: Bring down and delete the connection profile for '{{ interface }}'
include_tasks: run_role_with_clear_facts.yml
vars:
network_connections:
- name: "{{ connection_name.stdout }}"
persistent_state: absent
state: down