将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时看到它?
Ansible版本2.2.1.0 Python版本2.7.5 Testinfra版本1.6.4 Pytest版本3.1.3 – FRC
凡'zabbix_agent_version'定义,当你手动运行Ansible?这是来自远程主机上的自定义事实,还是设置在本地变量文件中? – larsks
zabbix_agent_version是在group_vars中定义的。当运行一个检查这个事实的剧本(debug:msg = {{zabbix_agent_version}})时,它可以通过Ansible获得。 – FRC