I've created a simple task which creates a fact (new variable):
---
- name: Define internal user based on prefix
set_fact:
role_user_uid: "{{ lookup('vars', '{{ user_prefix }}_uid', default='') }}"
where user_prefix
is defined in defaults/ as user_prefix: "ansible"
.
How can I create a test which checks that user_prefix
has a specific value?
I've tried via testinfra
@pytest.fixture()
def AnsibleVars(host):
all_vars = host.ansible.get_variables()
return all_vars
def test_user_exists(host,AnsibleVars):
print(AnsibleVars)
var_prefix = AnsibleVars['user_prefix']
and via ansible provider:
- name: "test default user"
assert:
that:
user_prefix == "ansible"
but I get errors that user_prefix
is note defined.
I did enable gather_facts: yes
in verify.yml
The way I handle this is to save all
vars
to a.yml
file at the end of theconverge
stage, so I can then later performpytest
tests based on the state of variables whenconverge
completes.The task to do this is placed in the
converge.yml
file for the scenario:To access these variables during
pytest
tests, do something along the lines of:Not perfect, but DRY and avoids hard-coding and coupling of tasks to tests.
Don't forget to add this to the
cleanup
stage:If anyone has a better way to do this, chime in! :)