文件构成是这样
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=True
ssh_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