Azure ansible - dynamic inventory not working

Viewed 858

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"
        ]
    }
}
1 Answers

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: (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 3:

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.

Related