文件构成是这样
testinfra/ |-ansible.cfg |-ssh_config |-test_myinfra.py各个文件构成
ansible.cfg
[ssh_connection] control_path = %(directory)s/%%h-%%r ssh_args = -o ControlPersist=15m -F ssh_config -q scp_if_ssh=Truessh_config
Host xxx.xxx.xxx.xxx
HostName xxx.xxx.xxx.xxx
User xxx
IdentityFile ~/.ssh/xxx.pem
Host 10*
User xxx
IdentityFile ~/.ssh/xxx.pem
ProxyCommand ssh -i ~/.ssh/xxx.pem -W %h:%p xxx@xxx.xxx.xxx.xxx
- test_myinfra.py
def test_apache_running_and_enabled(Service):
apache = Service("apache2")
assert apache.is_running
- 执行
AWS_PROFILE=xxx testinfra -v --connection=ansible --ansible-inventory=./ec2.py --host=tag_Name_xxx --ssh-config=ssh_config --sudo test_myinfra.py
本文介绍了一种使用Ansible进行自动化测试的方法。通过配置ansible.cfg和ssh_config文件实现远程主机的连接控制,并展示了如何编写test_myinfra.py来测试Apache服务的状态。最后提供了执行测试的具体命令。
323

被折叠的 条评论
为什么被折叠?



