Skip to main content
 首页 » 编程设计

python之将 Ansible 与 AWS 动态 list (ec2.py) 结合使用

2024年10月01日6fff_TT

尝试通过 ec2.py 使用静态+动态主机文件。

我已尝试遵循以下指南:

重要的是要说下面的运行是有效的!

ansible -m ping tag_Ansible_Test 

意味着 aws-cli、boto 和 ec2.py 已正确安装并正常工作。

现在,在主机文件中定义以下组(如文档所述)时:

[tag_Ansible_Test] 
 
[backend-test-group:children] 
tag_Ansible_Test 

ansible -m ping backend-test-group -vvvv 

尝试对组运行 ansible 命令时,出现以下错误:

ansible 2.4.0.0 
  config file = /etc/ansible/ansible.cfg 
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] 
  ansible python module location = /usr/lib/python2.7/site-packages/ansible 
  executable location = /bin/ansible 
  python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] 
Using /etc/ansible/ansible.cfg as config file 
setting up inventory plugins 
Parsed /etc/ansible/ec2.py inventory source with script plugin 
 [WARNING]: Could not match supplied host pattern, ignoring: backend-test-group 
 
 [WARNING]: No hosts matched, nothing to do 
 
Loading callback plugin minimal of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc 
 

我在这里错过了什么?

请您参考如下方法:

通过如下设置文件夹结构解决了问题:

/etc/ansible/combined 
- hosts (the original file static+dynamic hosts file) 
- ec2.py 
- ec2.ini 

然后,更改 ansible.cfg 中的 inventory 设置:

... 
inventory       = /etc/ansible/combined 
... 

感谢@KonstantinSuvorov 的帮助!