标签: testinfra

在 testinfra 中使用 Ansible 变量

使用带有 Ansible 后端的 TestInfra 进行测试。除了在运行测试时使用 Ansible 本身,一切都很好

测试文件

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"]
    (...)
Run Code Online (Sandbox Code Playgroud)

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

- hosts: all
  become: true
  tasks:
  - name: debug
    debug: msg={{ zabbix_agent_version }}
Run Code Online (Sandbox Code Playgroud)

命令执行测试

pytest --connection=ansible --ansible-inventory=inventory  --hosts=$hosts -v test.py
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

self = <ansible>, module_name = 'debug', module_args = 'msg={{ zabbix_agent_version }}', …
Run Code Online (Sandbox Code Playgroud)

python testing pytest ansible testinfra

7
推荐指数
1
解决办法
6416
查看次数

Accessing Ansible variables in molecule test, TestInfra

I picked up molecule while researching around inspec and how to use it in ansible. I found molecule very cool and adopted it. I wanted to use it in 2 ways.

1- When developing a role or playbook
2- After a particular playbook have been run on production.
Run Code Online (Sandbox Code Playgroud)

On number 1: I found this very useful question/ressponse on stackoverflow and that has helped me shape my thinking.I put my variable file for the role kafka under group_vars/all as suggested …

ansible molecule testinfra

5
推荐指数
0
解决办法
2554
查看次数

标签 统计

ansible ×2

testinfra ×2

molecule ×1

pytest ×1

python ×1

testing ×1