Using Ansible variables in testinfra


Using TestInfra with Ansible backend for testing purposes. Everything goes fine except using Ansible itself while running tests

import pytest
def test_zabbix_agent_package(host):
    package = host.package("zabbix-agent")
    assert package.is_installed
    package_version = host.ansible("debug", "msg={{ zabbix_agent_version }}")["msg"]

where zabbix_agent_version is an Ansible variable from group_vars. It can be obtained by running this playbook

- hosts: all
  become: true
  - name: debug
    debug: msg={{ zabbix_agent_version }}

command executing tests

pytest --connection=ansible --ansible-inventory=inventory  --hosts=$hosts -v


timeout = 10
host_key_checking = False
retry_files_enabled = False

the output I get is

self = <ansible>, module_name = 'debug', module_args = 'msg={{ zabbix_agent_version }}', check = True, kwargs = {}
result = {'failed': True, 'msg': "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'zabbix_agent_version' is undefined"}

    def __call__(self, module_name, module_args=None, check=True, **kwargs):
        if not self._host.backend.HAS_RUN_ANSIBLE:
            raise RuntimeError((
                "Ansible module is only available with ansible "
                "connection backend"))
        result = self._host.backend.run_ansible(
            module_name, module_args, check=check, **kwargs)
        if result.get("failed", False) is True:
>           raise AnsibleException(result)
E           AnsibleException: Unexpected error: {'failed': True,
E            'msg': u"the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'zabbix_agent_version' is undefined"}

/usr/lib/python2.7/site-packages/testinfra/modules/ AnsibleException

Any idea why Ansible can't see this variable when running testinfra's Ansible module while it can see it while running Ansible alone?

8/27/2019 10:22:47 AM

If zabbix_agent_version is a variable set using group_vars, then it seems as if you should be accessing it using host.ansible.get_variables() rather than running debug task. In any case, both should work. If I have, in my current directory:

And in group_vars/all.yml I have:

myvar: value

And in I have:

def test_myvar_using_get_variables(host):
    all_variables = host.ansible.get_variables()
    assert 'myvar' in all_variables
    assert all_variables['myvar'] == 'myvalue'

def test_myvar_using_debug_var(host):
    result = host.ansible("debug", "var=myvar")
    assert 'myvar' in result
    assert result['myvar'] == 'myvalue'

def test_myvar_using_debug_msg(host):
    result = host.ansible("debug", "msg={{ myvar }}")
    assert 'msg' in result
    assert result['msg'] == 'myvalue'

Then all tests pass:

$ py.test --connection=ansible --ansible-inventory=hosts -v 
============================= test session starts ==============================
platform linux2 -- Python 2.7.13, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 -- /home/lars/env/common/bin/python2
cachedir: .cache
rootdir: /home/lars/tmp/testinfra, inifile:
plugins: testinfra-1.8.1.dev2
collected 3 items                                                       [ansible://localhost] PASSED[ansible://localhost] PASSED[ansible://localhost] PASSED

=========================== 3 passed in 1.77 seconds ===========================

Can you confirm that the layout of our files (in particular, the location of your group_vars directory relative to the your tests) matches what I've shown here?

10/17/2017 4:20:25 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow