2017-10-17 71 views
1

将TestInfra与Ansible后端一起使用以用于测试目的。除了在运行测试在testinfra中使用Ansible变量

test.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使用可变Ansible本身一切顺利。它可以通过运行这个剧本

- 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时看到它?

+0

Ansible版本2.2.1.0 Python版本2.7.5 Testinfra版本1.6.4 Pytest版本3.1.3 – FRC

+0

凡'zabbix_agent_version'定义,当你手动运行Ansible?这是来自远程主机上的自定义事实,还是设置在本地变量文件中? – larsks

+0

zabbix_agent_version是在group_vars中定义的。当运行一个检查这个事实的剧本(debug:msg = {{zabbix_agent_version}})时,它可以通过Ansible获得。 – FRC

回答

1

如果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目录相对于您的测试的位置)与我在此处显示的内容相匹配?

+0

是的,我的布局与您演示的布局相同。当进行进一步的测试时,奇怪的是,当我试图获得Ansible内置的一些事实时(比如ansible_ssh_host),testinfra可以毫无问题地发现它,但是当我尝试一些由我的group_vars设置的东西时,它无法找到它虽然运行调试Ansible可以看到它。 – FRC

0

我追了几天的答案。这是最后为我工作的。基本上你使用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文件中可以解决该错误。