azure.azcollection.azure_kql inventory – Azure Resource Manager inventory plugin using Graph QL
Note
This inventory plugin is part of the azure.azcollection collection (version 3.8.0).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install azure.azcollection
.
You need further requirements to be able to use this inventory plugin,
see Requirements for details.
To use it in a playbook, specify: azure.azcollection.azure_kql
.
New in azure.azcollection 3.7.0
Synopsis
Query VM details from Azure Resource Manager using Graph QL
See https://learn.microsoft.com/en-us/azure/virtual-machines/resource-graph-samples?tabs=azure-cli for how to craft your own query. The one requirement is that you need to provide inventory_hostname.
Requires a YAML configuration file whose name ends with ‘azure_kql.(yml|yaml)’
Be aware that currently Azure Resource Graph may not be consistent with the actual state of your resources. It can take up to 30 minutes for updates to propagate. This applies both for resources to appear and to dissapear.
Requirements
The below requirements are needed on the local controller node that executes this inventory.
python >= 2.7
The host that executes this module must have the azure.azcollection collection installed via galaxy
All python packages listed in collection’s requirements.txt must be installed via pip on the host that executes modules from azure.azcollection
Full installation instructions may be found https://galaxy.ansible.com/azure/azcollection
Parameters
Parameter |
Comments |
---|---|
Active Directory username. Use when authenticating with an Active Directory user rather than service principal. |
|
Azure AD authority url. Use when authenticating with Username/password, and has your own ADFS authority. |
|
Selects an API profile to use when communicating with Azure services. Default value of Default: |
|
Controls the source of the credentials to use for authentication. Can also be set via the When set to When set to When set to When set to When set to The Choices:
|
|
Toggle to enable/disable the caching of the inventory’s source data, requires a cache plugin setup to work. Choices:
Configuration:
|
|
Cache connection data or path, read cache plugin documentation for specifics. Configuration:
|
|
Cache plugin to use for the inventory’s source data. Default: Configuration:
|
|
Prefix to use for cache plugin files/tables. Default: Configuration:
|
|
Cache duration in seconds. Default: Configuration:
|
|
Controls the certificate validation behavior for Azure endpoints. By default, all modules will validate the server certificate, but when an HTTPS proxy is in use, or against Azure Stack, it may be necessary to disable this behavior by passing Choices:
|
|
Azure client ID. Use when authenticating with a Service Principal or Managed Identity (msi). Can also be set via the |
|
For cloud environments other than the US public cloud, the environment name (as defined by Azure Python SDK, eg, Default: |
|
A mapping of hostvar names to Jinja2 expressions. The value for each host is the result of the Jinja2 expression (which may refer to any of the host’s existing variables at the time this inventory plugin runs). Default: |
|
Determines whether or not instance discovery is performed when attempting to authenticate. Setting this to true will completely disable both instance discovery and authority validation. This functionality is intended for use in scenarios where the metadata endpoint cannot be reached such as in private clouds or Azure Stack. The process of instance discovery entails retrieving authority metadata from https://login.microsoft.com/ to validate the authority. By setting this to **True**, the validation of the authority is disabled. As a result, it is crucial to ensure that the configured authority host is valid and trustworthy. Set via credential file profile or the Choices:
|
|
When false, template failures during group and filter processing are silently ignored (eg, if a filter or group expression refers to an undefined host variable) Choices:
|
|
A graph query which will retrieve the inventory of hosts you are interested in. You must return inventory_hostname as a field from your query. |
|
A mapping of group names to Jinja2 expressions. When the mapped expression is true, the host is added to the named group. Default: |
|
A list of Jinja2 expressions in order of precedence to compose inventory_hostname. Ignores expression if result is an empty string or None value. An expression of Use Default: |
|
Creates groups based on the value of a host variable. Requires a list of dictionaries, defining Default: |
|
The default value when the host variable’s value is This option is mutually exclusive with |
|
The key from input dictionary used to generate groups. |
|
parent group for keyed group. |
|
A keyed group name will start with this prefix. Default: |
|
separator used to build the keyed group name. Default: |
|
Set this option to This option is mutually exclusive with Choices:
|
|
Use in conjunction with By default, a keyed group that does not have a prefix or a separator provided will have a name that starts with an underscore. This is because the default prefix is Set this option to If the group name is derived from a mapping the separator is still used to concatenate the items. To not use a separator in the group name at all, set the separator for the keyed group to an empty string instead. Choices:
|
|
Parent argument. |
|
Parent argument. |
|
Active Directory user password. Use when authenticating with an Active Directory user rather than service principal. |
|
marks this as an instance of the ‘azure_rm’ plugin Choices:
|
|
Security profile found in ~/.azure/credentials file. |
|
Azure client secret. Use when authenticating with a Service Principal. |
|
If Since it is possible to use facts in the expressions they might not always be available and we ignore those errors by default. Choices:
|
|
Your Azure subscription Id. |
|
Azure tenant ID. Use when authenticating with a Service Principal. |
|
The thumbprint of the private key specified in x509_certificate_path. Use when authenticating with a Service Principal. Required if x509_certificate_path is defined. |
|
Merge extra vars into the available variables for composition (highest precedence). Choices:
Configuration:
|
|
Path to the X509 certificate used to create the service principal in PEM format. The certificate must be appended to the private key. Use when authenticating with a Service Principal. |
Note
Configuration entries listed above for each entry type (Ansible variable, environment variable, and so on) have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up. The entry types are also ordered by precedence from low to high priority order. For example, an ansible.cfg entry (further up in the list) is overwritten by an Ansible variable (further down in the list).
Notes
Note
For authentication with Azure you can pass parameters, set environment variables, use a profile stored in ~/.azure/credentials, or log in before you run your tasks or playbook with
az login
.Authentication is also possible using a service principal or Active Directory user.
To authenticate via service principal, pass subscription_id, client_id, secret and tenant or set environment variables AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_SECRET and AZURE_TENANT.
To authenticate via Active Directory user, pass ad_user and password, or set AZURE_AD_USER and AZURE_PASSWORD in the environment.
Alternatively, credentials can be stored in ~/.azure/credentials. This is an ini file containing a [default] section and the following keys: subscription_id, client_id, secret and tenant or subscription_id, ad_user and password. It is also possible to add additional profiles. Specify the profile by passing profile or setting AZURE_PROFILE in the environment.
Inventories are not finalized at this stage, so the auto populated
all
andungrouped
groups will only reflect what previous inventory sources explicitly added to them.Runtime ‘magic variables’ are not available during inventory construction. For example,
groups
andhostvars
do not exist yet.
See Also
See also
- Sign in with Azure CLI
How to authenticate using the
az login
command.
Examples
plugin: azure.azcollection.azure_kql
graph_query: |-
Resources
| where type =~ 'microsoft.compute/virtualmachines'
| project vmId = tolower(tostring(id)),
inventory_hostname = name,
tags,
location,
resourceGroup,
osType = tostring(properties.storageProfile.osDisk.osType),
powerState = tostring(properties.extended.instanceView.powerState.displayStatus),
hostName = properties.osProfile.computerName,
subscription_id = subscriptionId
| join kind=inner (ResourceContainers
| where type=='microsoft.resources/subscriptions'
| extend subscription_name = name,
subscription_id = subscriptionId,
state = properties.state
| where state == 'Enabled'
| project subscription_name,
subscription_id)
on subscription_id
| project-away subscription_id1
| join (Resources
| where type =~ 'microsoft.network/networkinterfaces'
| mv-expand ipconfig=properties.ipConfigurations
| project vmId = tolower(tostring(properties.virtualMachine.id)),
privateIp = ipconfig.properties.privateIPAddress,
publicIpId = tostring(ipconfig.properties.publicIPAddress.id)
| join kind=leftouter (Resources
| where type =~ 'microsoft.network/publicipaddresses'
| project publicIpId = id, publicIp = properties.ipAddress
) on publicIpId
| project-away publicIpId, publicIpId1
| summarize privateIps = make_list(privateIp), publicIps = make_list(publicIp) by vmId
) on vmId
| project-away vmId1
| sort by inventory_hostname asc
# adds variables to each host found by this inventory plugin, whose values are the result of the associated expression
compose:
ansible_host: "(publicIps + privateIps) | first"
ansible_winrm_kerberos_hostname_override: "inventory_name + '.domain.tld'"
ansible_winrm_transport: "'ntlm' if ('AAP_managed' in tags and ('DMZ' in (tags.AAP_Managed|list) or 'Local' in (tags.AAP_Managed|list))) else 'kerberos'"
groups:
AAP_Managed: "'AAP_Managed' in (tags|list)"
ubuntu18: "'AAP_Managed' in (tags|list) and 'ubuntu18' in tags.Ansible_OS"
ubuntu20: "'AAP_Managed' in (tags|list) and 'ubuntu20' in tags.Ansible_OS"
ubuntu22: "'AAP_Managed' in (tags|list) and 'ubuntu22' in tags.Ansible_OS"
rhel7: "'AAP_Managed' in (tags|list) and 'rhel7' in tags.Ansible_OS"
rhel8: "'AAP_Managed' in (tags|list) and 'rhel8' in tags.Ansible_OS"
rhel9: "'AAP_Managed' in (tags|list) and 'rhel9' in tags.Ansible_OS"
windows2012: "'AAP_Managed' in (tags|list) and 'windows2012' in tags.Ansible_OS"
windows2016: "'AAP_Managed' in (tags|list) and 'windows2016' in tags.Ansible_OS"
windows2019: "'AAP_Managed' in (tags|list) and 'windows2019' in tags.Ansible_OS"
windows2022: "'AAP_Managed' in (tags|list) and 'windows2022' in tags.Ansible_OS"
Asia: "'AAP_managed' in (tags|list) and 'Asia' in tags.AAP_Managed"
North_America: "'AAP_managed' in (tags|list) and 'Asia' not in tags.AAP_Managed"
# change how inventory_hostname is generated. Each item is a jinja2 expression similar to hostvar_expressions.
hostnames:
- "tags.vm_name if 'vm_name' in tags"
- default_inventory_hostname + ".domain.tld" # Transfer to fqdn if you use shortnames for VMs
- default # special var that uses the default hashed name
keyed_groups:
- prefix: ""
separator: ""
key: osType
- prefix: ""
separator: ""
key: location
- prefix: ""
separator: ""
key: powerState