使用带有 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) 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 …