Skip to content

Commit 4540a74

Browse files
committed
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>
1 parent 01be895 commit 4540a74

12 files changed

Lines changed: 50 additions & 15 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
# Role Name
1+
# trustee_attestation_server
22

33
[![ansible-lint.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/ansible-lint.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/ansible-lint.yml) [![ansible-test.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/ansible-test.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/ansible-test.yml) [![codespell.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/codespell.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/codespell.yml) [![markdownlint.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/markdownlint.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/markdownlint.yml) [![qemu-kvm-integration-tests.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/qemu-kvm-integration-tests.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/qemu-kvm-integration-tests.yml) [![shellcheck.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/shellcheck.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/shellcheck.yml) [![tft.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/tft.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/tft.yml) [![tft_citest_bad.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/tft_citest_bad.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/tft_citest_bad.yml) [![woke.yml](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/woke.yml/badge.svg)](https://github.com/linux-system-roles/trustee_attestation_server/actions/workflows/woke.yml)
44

5-
![template](https://github.com/linux-system-roles/template/workflows/tox/badge.svg)
5+
![trustee_attestation_server](https://github.com/linux-system-roles/trustee_attestation_server/workflows/tox/badge.svg)
66

7-
A template for an ansible role that configures some GNU/Linux subsystem or
8-
service. A brief description of the role goes here.
7+
An Ansible role that manages trustee attestation servers.
98

109
## Requirements
1110

examples/simple.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# SPDX-License-Identifier: MIT
22
---
3-
- name: Example template role invocation
3+
- name: Example trustee_attestation_server role invocation
44
hosts: all
55
vars:
6-
template_foo: example variable value
7-
template_bar: false
6+
trustee_attestation_server_foo: example variable value
7+
trustee_attestation_server_bar: false
88
roles:
9-
- linux-system-roles.template
9+
- linux-system-roles.trustee_attestation_server

tests/roles/linux-system-roles.template/defaults renamed to tests/roles/linux-system-roles.trustee_attestation_server/defaults

File renamed without changes.

tests/roles/linux-system-roles.template/handlers renamed to tests/roles/linux-system-roles.trustee_attestation_server/handlers

File renamed without changes.
File renamed without changes.

tests/roles/linux-system-roles.template/tasks renamed to tests/roles/linux-system-roles.trustee_attestation_server/tasks

File renamed without changes.

tests/roles/linux-system-roles.template/templates renamed to tests/roles/linux-system-roles.trustee_attestation_server/templates

File renamed without changes.
File renamed without changes.

tests/setup-snapshot.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
tasks:
55
- name: Set platform/version specific variables
66
include_role:
7-
name: linux-system-roles.template
7+
name: linux-system-roles.trustee_attestation_server
88
tasks_from: set_vars.yml
99
public: true
1010

1111
- name: Install test packages
1212
package:
13-
name: "{{ __template_packages }}"
13+
name: "{{ __trustee_attestation_server_packages }}"
1414
state: present
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# Task file: clear_facts, run linux-system-roles.trustee_attestation_server.
3+
# Include this with include_tasks or import_tasks
4+
# Input:
5+
# - __sr_tasks_from: tasks_from to run - same as tasks_from in include_role
6+
# - __sr_public: export private vars from role - same as public in include_role
7+
# - __sr_failed_when: set to false to ignore role errors - same as failed_when in include_role
8+
- name: Clear facts
9+
meta: clear_facts
10+
11+
# note that you can use failed_when with import_role but not with include_role
12+
# so this simulates the __sr_failed_when false case
13+
# Q: Why do we need a separate task to run the role normally? Why not just
14+
# run the role in the block and rethrow the error in the rescue block?
15+
# A: Because you cannot rethrow the error in exactly the same way as the role does.
16+
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
17+
- name: Run the role with __sr_failed_when false
18+
when:
19+
- __sr_failed_when is defined
20+
- not __sr_failed_when
21+
block:
22+
- name: Run the role
23+
include_role:
24+
name: linux-system-roles.trustee_attestation_server
25+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
26+
public: "{{ __sr_public | default(false) }}"
27+
rescue:
28+
- name: Ignore the failure when __sr_failed_when is false
29+
debug:
30+
msg: Ignoring failure when __sr_failed_when is false
31+
32+
- name: Run the role normally
33+
include_role:
34+
name: linux-system-roles.trustee_attestation_server
35+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
36+
public: "{{ __sr_public | default(false) }}"
37+
when: __sr_failed_when | d(true)

0 commit comments

Comments
 (0)