gpt4 book ai didi

python - 在 testinfra 中使用 Ansible 变量

转载 作者:行者123 更新时间:2023-11-28 19:52:09 27 4
gpt4 key购买 nike

使用带有 Ansible 后端的 TestInfra 进行测试。除了在运行测试时使用 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 时却可以看到它吗?

最佳答案

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

关于python - 在 testinfra 中使用 Ansible 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46792370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com