Implementing your own Ansible Azure Collection

Introduction

In a recent azure ansible article I wrote (https://adrianhynes.medium.com/orchestrating-azure-resources-with-ansible-fa82f4e3dfd6), I came across a challenge, whereby listing resources in a resource group wasn’t real-time.

In that article I improvised by waiting and checking every minute until the resources eventually appeared. BUT that loop and sleep took ~20 minutes.

It turns out, the “/resources API is served from a regional cache from Azure Resource Manager, which is eventual consistent, and not real time” https://github.com/Azure/AKS/issues/1964

This means, in order to get a real time view of the resources in our resource group, we need to hit the regional azure management endpoint i.e. https://<region>.management.azure.com/

In this article, I want to show a simple way I’ve got around this by creating our own custom module.

Updating the Azure Ansible Collection

I’m going to do this through Azure Cloudshell, so at the time of writing, Ansible on Cloudshell is using Python 2.7, so first let’s install ansible for python 3

pip install ansible

Clone down the latest Azure Ansible Collection

git clone https://github.com/ansible-collections/azure.git

Install all the azure ansible collection dependencies

pip install -r azure/requirements-azure.txt

I’m going to create a new module, by coping azure_rm_resource_info.py

cp azure/plugins/modules/azure_rm_resource_info.py azure/plugins/modules/azure_rm_resource_info_rt.py

Now we’ll make a few changes, highlighted in bold

class AzureRMResourceInfoRT(AzureRMModuleBase)...self.module_arg_spec = dict(
url=dict(type='str'),
provider=dict(type='list'),
...
location=dict(type='str')
)
...
self.location = None...
def exec_module(self, **kwargs):
...
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, base_url="https://"+self.location+".management.azure.com/")

See the full code for this module here: https://github.com/aido123/ansible/blob/main/azure_rm_resource_info_rt.py

Right we’re good to install/upgrade this Azure Ansible Collection.

#Generate collection tarball (--force to replace if ran previously)
ansible-galaxy collection build --force
#Install the collection tarball (--force to reinstall)
ansible-galaxy collection install azure-azcollection-*.tar.gz --force

Now let’s give it a run. Create a simple Playbook called ans.yaml

- name: AKS Demo Ansible Playbook on Azure Cloud Shell
hosts: localhost
tasks:
- name: List all VMScaleSets in the rsg1 Resource Group
azure.azcollection.azure_rm_resource_info_rt:
resource_group: rsg1
provider: compute
resource_type: virtualmachinescalesets
api_version: "2017-12-01"
location: northeurope
register: vmssresources
until: vmssresources.response[0].name is defined
retries: 20
delay: 60
- debug:
var: vmssresources

Now run the playbook

ansible-playbook ans.yaml

Conclusion

I hope you found this article useful and it gives you some ideas on creating your own azure ansible collection. Obviously you don’t want to have to reinstall the azure ansible module each time, so preferably you will create your own collection (extend azure_rm_common.py) that you can install independently.

--

--

--

Cloud Platform Architect. Opinions and articles on medium are my own.

Love podcasts or audiobooks? Learn on the go with our new app.

Set up MAMP php on macOS

10 KEY SKILLS A GOOD PROGRAMMER SHOULD POSSES! : Tech and innovation Hub

Permutations of string

How to Make Better Use of SVG in Flutter Applications?

Scaling Data Engineering using Scala Implicits and Ad-hoc Polymorphism with Apache Spark

Managing an IT project team is like playing Dota2

Learning a new language: programming

Memory Leaks in Swift

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrian Hynes

Adrian Hynes

Cloud Platform Architect. Opinions and articles on medium are my own.

More from Medium

Install WSL-Ubuntu 20.04 in other non-C drive

Creating a Minecraft Plugin

Implementing Password History in Azure AD B2C

Setup SonarQube & SonarScanner on MacOS