0

I'm running Ansible using AWX, I have configured Azure Credentials and I'm able to use Azure Modules to create/delete Azure Resources.

However Ansible is lacking some of the modules that I need, therefore I wanted to run direct commands using Azure CLI however every time I receive a result that there is no such command.

How Ansible/AWX/Tower is executing commands from Azure Modules?

Running code below against localhost:

hosts: localhost

#

#This part is working    
- name: Create a network interface with minimal parameters
       azure_rm_networkinterface:
        name: "{{ vm_name }}-nic"
        resource_group: "{{ internal_name }}-{{ customer_name }}"
        virtual_network: "{{ internal_name }}-vnet"
        subnet_name: "{{ internal_name }}-subnet01"
        security_group: "{{ internal_name }}-subnet01-nsg" 
#This Part is not working     
    - name: Check Azure Version
      shell: az --version
#The same playbook

Output from the job is that 1st part did some changes and 2nd one give an error:

"stderr": "/bin/sh: az: command not found",

Any idea how to works with Azure CLI in Ansible AWX/Tower

user3574248
  • 167
  • 1
  • 3
  • 10
  • Is the `az` command installed on your local host? If so, what is the full path to the `az` command? What if you use the full path in your playbook instead of the unqualified name? – larsks Aug 22 '19 at 13:13
  • @larsks - No idea what did you mean by that"What if you use the full path in your playbook instead of the unqualified name" Ansible Azure module somehow is able to perform things in Azure, how it's done? – user3574248 Aug 23 '19 at 06:45

3 Answers3

0

I have resolved with ARM templates using Ansible.

However, CLI could be an easier way.

user3574248
  • 167
  • 1
  • 3
  • 10
0

You need to install azure cli on the host before you can use it. Installation instructions here: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest

The following playbook should do it for you too:

- name: Install Azure Cli
  hosts: localhost
  gather_facts: true
  become: true
  tasks:
    - block: #Install az cli on CentOs/RHEL
      - name: Import the APT repo key (Debian)
        shell: rpm --import https://packages.microsoft.com/keys/microsoft.asc

      - name: Create local azure-cli repository information
        shell: sh -c 'echo -e "[azure-cli]\nname=Azure CLI\nbaseurl=https://packages.microsoft.com/yumrepos/azure-cli\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/azure-cli.repo'
      when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'

    - block: #Install az cli on Debian/Ubuntu
      - name: Run MS install script
        shell: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
      when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

    - name: Install Azure CLi
      package:
        name: "azure-cli"
        state: present

    - name: Verify installed version
      shell: az --version
      register: azureVersion

    - debug:
        msg: "{{azureVersion.stdout}}"  
Revive
  • 2,248
  • 1
  • 16
  • 23
0

To use Ansible with Azure Cli, you need to install several packages. Some changes were made in Azure Cli packages, and there are now two different packages that can be installed. To work with Ansible version 2.12.2 on Ubuntu 20.04.4 LTS you can do the following for installing Ansible and Azure Cli:

sudo apt install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get install ansible
ansible-galaxy collection install azure.azcollection
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements-azure.txt

Next you have at least two options:

  • az login (it will open a browser so you can do the login
  • use environment variables

In case of environment variables you can use the following:

export AZURE_SUBSCRIPTION_ID=
export AZURE_TENANT=
export AZURE_AD_USER=
export AZURE_PASSWORD=

More information over connecting to Azure: https://docs.ansible.com/ansible/latest/scenario_guides/guide_azure.html

An example of creating a VM on Azure using Ansible: https://learn.microsoft.com/en-us/azure/developer/ansible/vm-configure?tabs=ansible

pringi
  • 3,987
  • 5
  • 35
  • 45