I've configured the credentials and I can run playbooks to create vm's. But I can't list the inventory using the dynamic inventory plugin. I have azure_rm.py
in the same directory.
azure_rm.yml
plugin: azure_rm
include_vm_resource_groups:
- readit-app-rg
auth_source: auto
ansible-inventory -vvvv -i myazure_rm.yml --list
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"
]
}
}
CodePudding user response:
Run the following command to query the VMs within the resource group:
ansible-inventory -i myazure_rm.yml --graph
When you run the command, you see results similar to the following output:
@all:
|--@ungrouped:
| |--linux-vm_cdb4
| |--win-vm_3211
Developing Dynamic Inventory Sources
If you intend to replace an existing inventory ini file with a dynamic provider, it must return a JSON object which contains an ‘all’ group that includes every host in the inventory as a member and every group in the inventory as a child. It should also include an ‘ungrouped’ group which contains all hosts which are not members of any other group. A skeleton example of this JSON object is:
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {}
}
Thank you logston. Posting your suggestion as an answer to help other community members.
inventory
[localhost]
127.0.0.1 ansible_connection=local
ansible.cfg
[defaults]
inventory = inventory
myazure_rm.yml
---
- hosts: localhost
tasks:
- name: .....
Test command:
ansible-playbook --list-hosts myazure_rm.yml
You can refer to Configure dynamic inventories of your Azure resources using Ansible, Ansible :Unable to parse /etc/ansible/hosts as an inventory source and Dynamic inventory in yaml format
If you still get the empty groups/ungroups, you can refer to GitHub issues: Empty groups from vars plugins inconsistently shown and Empty groups ignored when sourcing from projects
CodePudding user response:
Resolved the issues. There were a couple of problems.
Issue 1:
[WARNING]: Failed to load inventory plugin, skipping yml
got resolved after removing the [inventory]
and changing it to [defaults]
in ansible.cfg
[defaults]
inventory = test.azure_rm.yml
enable_plugins = host_list, script, auto, yaml, yml, ini, azure_rm
Issue 2:
[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
got resolved after adding ---
to the test.azure_rm.yml
.
---
plugin: azure_rm
include_vm_resource_groups:
- readit-app-rg
auth_source: auto
location: eastus
#keyed_groups:
# - prefix: Ansible
# key: Name
Issue 3: (not sure if this is an issue but I read that the file names should end with .azure.rm.yml
Renamed the file from myazure_rm.yml
to test.azure_rm.yml
Issue 4:
Only VM's created using Ansible playbook are displayed when we list the inventory using the command ansible-inventory -vvvv -i test.azure_rm.yml --list
.
In AWS we have ec2.py
and it lists all VM's from AWS console. But in Azure, I had few VM's created manually and the script was returning empty list. After I ran the playbook and created few VM's, the script returned only the VM's created by Ansible.
Note: Credentials should be stored in ~/.azure/credentials
or use az login
.