Ansible - Azure

> Infrastructure as code > Ansible

1 - Install

pip install 'ansible[azure]'
pip list
adal (1.2.0)
ansible (2.7.1)
applicationinsights (0.11.7)
argcomplete (1.9.4)
asn1crypto (0.24.0)
azure-cli-core (2.0.35)
azure-cli-nspkg (3.0.2)
azure-common (1.1.11)
azure-graphrbac (0.40.0)
azure-keyvault (1.0.0a1)
azure-mgmt-batch (4.1.0)
azure-mgmt-compute (2.1.0)
azure-mgmt-containerinstance (0.4.0)
azure-mgmt-containerregistry (2.0.0)
azure-mgmt-containerservice (3.0.1)
azure-mgmt-dns (1.2.0)
azure-mgmt-keyvault (0.40.0)
azure-mgmt-marketplaceordering (0.1.0)
azure-mgmt-monitor (0.5.2)
azure-mgmt-network (1.7.1)
azure-mgmt-nspkg (2.0.0)
azure-mgmt-rdbms (1.2.0)
azure-mgmt-resource (1.2.2)
azure-mgmt-sql (0.7.1)
azure-mgmt-storage (1.5.0)
azure-mgmt-trafficmanager (0.50.0)
azure-mgmt-web (0.32.0)
azure-nspkg (2.0.0)
azure-storage (0.35.1)
..................

2 - azure_rm.py

2.1 - Install

Ansible - Inventory in Azure

wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/azure_rm.py
chmod +x azure_rm.py
./azure_rm.py --help

There is also a azure_rm file but it can not be called directly

find / -name azure_rm.py
/usr/lib/python2.7/dist-packages/ansible/plugins/inventory/azure_rm.py
Advertising

2.2 - List info

# all host
azure_rm.py --list
# one host
azure_rm.py --host my_instance_host_name --resource-groups=Testing --pretty

2.3 - Executing command on all host

ansible \
    -i azure_rm.py resourceGroup \
    -m shell \
    -a "/bin/uname -a"

2.4 - Executing a playbook on all host

  • The playbook
test_azure_inventory.yml
- name: Test the inventory script
  hosts: azure
  connection: local
  gather_facts: no
  tasks:
    - debug: msg="{{ inventory_hostname }} has powerstate {{ powerstate }}"
  • Run
ansible-playbook -i azure_rm.py test_azure_inventory.yml
PLAY [Test the inventory script] *****************************

TASK [debug] ************************************************
ok: [DVM] => {
    "msg": "DVM has powerstate deallocated"
}
ok: [VM1] => {
    "msg": "VM1 has powerstate deallocated"
}
ok: [VM2] => {
    "msg": "VM2 has powerstate deallocated"
}
ok: [MSFT-DB-01] => {
    "msg": "MSFT-DB-01 has powerstate deallocated"
}
ok: [MSFT-WIN-01] => {
    "msg": "MSFT-WIN-01 has powerstate deallocated"
}

3 - Doc

Advertising