我已经配置了凭据,可以运行攻略来创建虚拟机,但不能使用动态清单插件列出清单。我在同一目录中有azure_rm.py。
azure_rm.yml
plugin: azure_rm
include_vm_resource_groups:
- readit-app-rg
auth_source: autoansible-inventory -vvvv -i myazure_rm.yml --列表
ansible-inventory [core 2.11.2]
config file = /home/ansible/azure_ansible/ansible.cfg
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible-inventory
python version = 3.8.5 (default, May 27 2021, 13:30:53) [GCC 9.3.0]
jinja version = 3.0.1
libyaml = True
Using /home/ansible/azure_ansible/ansible.cfg as config file
setting up inventory plugins
redirecting (type: inventory) ansible.builtin.azure_rm to azure.azcollection.azure_rm
Loading collection azure.azcollection from /usr/local/lib/python3.8/dist-packages/ansible_collections/azure/azcollection
[WARNING]: Failed to load inventory plugin, skipping yml
Skipping due to inventory source not existing or not being readable by the current user
ansible_collections.azure.azcollection.plugins.inventory.azure_rm declined parsing /home/ansible/azure_ansible/myazure_rm.yml as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /home/ansible/azure_ansible/myazure_rm.yml as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /home/ansible/azure_ansible/myazure_rm.yml as it did not pass its verify_file() method
[WARNING]: Unable to parse /home/ansible/azure_ansible/myazure_rm.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
}
}发布于 2021-10-20 09:29:32
执行以下命令查询资源组内的虚拟机:
ansible-inventory -i myazure_rm.yml --graph当您运行该命令时,您将看到类似于以下输出的结果:
@all:
|--@ungrouped:
| |--linux-vm_cdb4
| |--win-vm_3211Developing Dynamic Inventory Sources
如果您打算用动态提供程序替换现有的清单ini文件,它必须返回一个JSON对象,该对象包含一个‘’组,该组将清单中的每个主机都作为成员,并将清单中的每个组都作为子组。它还应包括一个“未分组”组,其中包含不属于任何其他组的所有主机。此JSON对象的框架示例如下:
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {}
}谢谢你logston。将您的建议作为答案发布,以帮助其他社区成员。
inventory
[localhost]
127.0.0.1 ansible_connection=localansible.cfg
[defaults]
inventory = inventorymyazure_rm.yml
---
- hosts: localhost
tasks:
- name: .....测试命令:
ansible-playbook --list-hosts myazure_rm.yml
你可以参考Configure dynamic inventories of your Azure resources using Ansible,Ansible :Unable to parse /etc/ansible/hosts as an inventory source和Dynamic inventory in yaml format
如果您仍然得到空组/解组,您可以参考GitHub问题:Empty groups from vars plugins inconsistently shown 和Empty groups ignored when sourcing from projects
发布于 2021-10-20 20:31:15
已解决问题。有几个问题。
问题1:
[WARNING]: Failed to load inventory plugin, skipping yml
在ansible.cfg中删除[inventory]并将其更改为[defaults]后得到解决
[defaults]
inventory = test.azure_rm.yml
enable_plugins = host_list, script, auto, yaml, yml, ini, azure_rm问题2:(不确定这是否是问题,但我读到文件名应该以.azure.rm.yml结尾
将文件从myazure_rm.yml重命名为test.azure_rm.yml
问题3:
当我们使用命令ansible-inventory -vvvv -i test.azure_rm.yml --list列出清单时,只显示使用Ansible playbook创建的VM。
在亚马逊网络服务中,我们有ec2.py,它列出了所有来自亚马逊网络服务控制台的虚拟机。但在Azure中,我手动创建的VM很少,脚本返回的是空列表。在我运行剧本并创建了几个VM之后,脚本只返回由Ansible创建的VM。
注意:凭证应存储在~/.azure/credentials中或使用az login。
https://stackoverflow.com/questions/69642975
复制相似问题