7

将 TestInfra 与 Ansible 后端一起用于测试目的。一切都很好,除了在运行测试时使用 Ansible 本身

测试.py

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"]
    (...)

其中 zabbix_agent_version 是来自 group_vars 的 Ansible 变量。可以通过运行这个 playbook 来获得

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

命令执行测试

pytest --connection=ansible --ansible-inventory=inventory  --hosts=$hosts -v test.py

ansible.cfg

[defaults]
timeout = 10
host_key_checking = False
library=library/
retry_files_enabled = False
roles_path=roles/
pipelining=true
ConnectTimeout=60
remote_user=deploy
private_key_file=/opt/jenkins/.ssh/deploy

我得到的输出是

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/ansible.py:70: AnsibleException

知道为什么 Ansible 在运行 testinfra 的 Ansible 模块时看不到这个变量,而在单独运行 Ansible 时可以看到它吗?

4

2 回答 2

6

如果zabbix_agent_version是使用 设置的变量group_vars,那么您似乎应该使用host.ansible.get_variables()而不是运行debug任务来访问它。无论如何,两者都应该工作。如果我有,在我的当前目录中:

test_myvar.py
group_vars/
  all.yml

group_vars/all.yml我有:

myvar: value

test_myvar.py我有:

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'

然后所有测试通过:

$ py.test --connection=ansible --ansible-inventory=hosts -v 
test_myvar.py 
============================= 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                                                               

test_myvar.py::test_myvar_using_get_variables[ansible://localhost] PASSED
test_myvar.py::test_myvar_using_debug_var[ansible://localhost] PASSED
test_myvar.py::test_myvar_using_debug_msg[ansible://localhost] PASSED

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

你能确认我们文件的布局(特别是你的group_vars目录相对于你的测试的位置)与我在这里显示的一致吗?

于 2017-10-17T16:20:25.830 回答
3

我追了几天这个答案。这就是最终对我有用的方法。本质上,您正在使用 testinfra 的 Ansible 模块来访问 Ansible 的 include_vars 函数。

import pytest

@pytest.fixture()
def AnsibleVars(host):
ansible_vars = host.ansible(
    "include_vars", "file=./group_vars/all/vars.yml")
return ansible_vars["ansible_facts"]

然后在我的测试中,我将该函数作为参数包含在内:

def test_something(host, AnsibleVars):

此解决方案部分取自https://github.com/metacloud/molecule/issues/151

我有一个有趣的问题,我试图从我的主剧本中包含变量,并且在包含 playbook.yml 文件时收到“必须存储为字典/哈希”的错误。将变量分离到 group_vars/all/vars.yml 文件中解决了该错误。

于 2017-11-30T14:55:28.190 回答