-
Notifications
You must be signed in to change notification settings - Fork 664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: accessing values of variables as they are being used for provisioning an instance inside Testinfra tests #151
Comments
Hi tjanez, We load ansible variables files (defaults and vars) with the following functions, maybe it works for you: @pytest.fixture(scope="module") @pytest.fixture(scope="module") Regards Raúl Melo |
Also, molecule executes |
@melodous, thanks for sharing your code, it was very valuable. I had to adapt your approach a bit since I want to create a role that loads different variables depending on value of import os.path
import pytest
@pytest.fixture()
def AnsibleDistribution(Ansible):
return Ansible("setup")["ansible_facts"]["ansible_distribution"]
@pytest.fixture()
def AnsiblePostgresqlInstallSource(Ansible):
return Ansible("debug", "msg={{ postgresql_install_source }}")["msg"]
@pytest.fixture()
def AnsibleVars(Ansible, AnsibleDistribution, AnsiblePostgresqlInstallSource):
if AnsibleDistribution == "fedora":
vars_file = "fedora.yml"
elif AnsibleDistribution in ["CentOS", "RedHat"]:
if AnsiblePostgresqlInstallSource == "centos_scl_repo":
vars_file = "centos_scl_repo.yml"
else:
vars_file = "RedHat.yml"
else:
raise ValueError("Unsupported distribution: " + AnsibleDistribution)
return Ansible("include_vars", os.path.join("./vars/", vars_file))["ansible_facts"]
def test_postgresql_running_and_enabled(Service, AnsibleVars):
postgresql = Service(AnsibleVars["postgresql_unit_name"])
assert postgresql.is_running
assert postgresql.is_enabled I had to modify However, this is not very usable yet since
Is it possible to specify additional variables to put into the inventory file for an instance at the level of |
Not currently, but welcome PRs. |
I had an use case in which I wanted to inspect variables set with Molecule converge playbook:
Note:
|
@Perdjesk Very nice! - name: dump
copy:
content: |
{{ vars | to_yaml }}
dest: /tmp/ansible-vars.yml |
Once #3313 got merged, you can use the following line to get all the vars defined for a given host (including host/group vars and recursive variable expansion):
ansible_vars = host.ansible('debug', 'msg={{ hostvars[inventory_hostname] }}')
print(ansible_vars['msg']['bar']) # Will output: foobar |
I want to use Testinfra tests to test my role.
Inside an Testinfra test I would like to access the values of variables as they are being used for provisioning the machine when
playbook.yml
is converged for some instance.I need this since the instance's state, which I want to check, depends on the chosen values of the variables defined in role's
default/main.yml
orvars/main.yml
file.I tried using the following 'trick' suggested by Testinfra maintainer, but it only works for Ansible facts and
group_vars
/host_vars
, not for variables defined within a role, either indefault/main.yml
orvars/main.yml
.Here is an example test for PostgreSQL service:
Variable
postgresql_unit_name
is defined in role'svars/main.yml
, but apparently the invocation of Ansible through Testinfra is unable to find it. Here is the error:Any ideas how I could achieve this?
The text was updated successfully, but these errors were encountered: