首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Azure ansible -动态清单不工作

Azure ansible -动态清单不工作
EN

Stack Overflow用户
提问于 2021-10-20 08:50:25
回答 2查看 375关注 0票数 0

我已经配置了凭据,可以运行攻略来创建虚拟机,但不能使用动态清单插件列出清单。我在同一目录中有azure_rm.py

azure_rm.yml

代码语言:javascript
运行
复制
plugin: azure_rm
include_vm_resource_groups:
  - readit-app-rg
auth_source: auto

ansible-inventory -vvvv -i myazure_rm.yml --列表

代码语言:javascript
运行
复制
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"
        ]
    }
}
EN

回答 2

Stack Overflow用户

发布于 2021-10-20 09:29:32

执行以下命令查询资源组内的虚拟机:

代码语言:javascript
运行
复制
ansible-inventory -i myazure_rm.yml --graph

当您运行该命令时,您将看到类似于以下输出的结果:

代码语言:javascript
运行
复制
@all:
  |--@ungrouped:
  |  |--linux-vm_cdb4
  |  |--win-vm_3211

Developing Dynamic Inventory Sources

如果您打算用动态提供程序替换现有的清单ini文件,它必须返回一个JSON对象,该对象包含一个‘’组,该组将清单中的每个主机都作为成员,并将清单中的每个组都作为子组。它还应包括一个“未分组”组,其中包含不属于任何其他组的所有主机。此JSON对象的框架示例如下:

代码语言:javascript
运行
复制
{
        "_meta": {
                "hostvars": {}
        },
        "all": {
                "children": [
                        "ungrouped"
                ]
        },
        "ungrouped": {}
}

谢谢你logston。将您的建议作为答案发布,以帮助其他社区成员。

inventory

代码语言:javascript
运行
复制
[localhost]
127.0.0.1  ansible_connection=local

ansible.cfg

代码语言:javascript
运行
复制
[defaults]
inventory = inventory

myazure_rm.yml

代码语言:javascript
运行
复制
---

- hosts: localhost
  tasks:
    - name: .....

测试命令:

ansible-playbook --list-hosts myazure_rm.yml

你可以参考Configure dynamic inventories of your Azure resources using AnsibleAnsible :Unable to parse /etc/ansible/hosts as an inventory sourceDynamic inventory in yaml format

如果您仍然得到空组/解组,您可以参考GitHub问题:Empty groups from vars plugins inconsistently shown Empty groups ignored when sourcing from projects

票数 0
EN

Stack Overflow用户

发布于 2021-10-20 20:31:15

已解决问题。有几个问题。

问题1:

[WARNING]: Failed to load inventory plugin, skipping yml

在ansible.cfg中删除[inventory]并将其更改为[defaults]后得到解决

代码语言:javascript
运行
复制
[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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69642975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档