Install the Azure cli tool

Ansible

# az_cli.yaml

--- # ansible-playbook az_cli.yaml --ask-become-pass
- name: Install Azure CLI
  become: yes
  connection: local
  hosts: localhost

  tasks:
    - name: Ensure dependencies are installed
      apt:
        name:
          - ca-certificates
          - curl
          - apt-transport-https
          - lsb-release
          - gnupg
        state: present
      when: ansible_os_family == "Debian"

    - name: Add Microsoft signing key (Debian/Ubuntu)
      apt_key:
        url: https://packages.microsoft.com/keys/microsoft.asc
        state: present
      when: ansible_os_family == "Debian"

    - name: Add Azure CLI repository (Debian/Ubuntu)
      apt_repository:
        repo: "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ {{ ansible_distribution_release }} main"
        state: present
      when: ansible_os_family == "Debian"

    - name: Install Azure CLI (Debian/Ubuntu)
      apt:
        name: azure-cli
        state: latest
      when: ansible_os_family == "Debian"

    - name: Ensure dnf dependencies are installed (RHEL/Fedora)
      dnf:
        name:
          - ca-certificates
          - curl
          - rpm
          - gpg
        state: present
      when: ansible_os_family == "RedHat"

    - name: Add Microsoft repo (RHEL/Fedora)
      get_url:
        url: https://packages.microsoft.com/config/rhel/9/prod.repo
        dest: /etc/yum.repos.d/azure-cli.repo
      when: ansible_os_family == "RedHat"

    - name: Install Azure CLI (RHEL/Fedora)
      dnf:
        name: azure-cli
        state: latest
      when: ansible_os_family == "RedHat"
ansible-playbook az_cli.yaml --ask-become-pass

Containers

Ephemeral container

Create a volume

podman create volume az-config

Pull the container image

podman pull mcr.microsoft.com/azure-cli:latest

Set an alias

alias "az=podman run --rm --entrypoint az -it -v az-config:/root/.azure:Z mcr.microsoft.com/azure-cli:latest"

Make the alias permanent:

echo "alias 'az=podman run --rm --entrypoint az -it -v az-config:/root/.azure:Z mcr.microsoft.com/azure-cli:latest'" >> ~/.bashrc.d/cloud_aliases.sh

Initialize a configuration

az login

Compose file

# compose.yaml

services:
  awscli:
    image: mcr.microsoft.com/azure-cli:latest
    container_name: az
    volumes:
      - az-config:/root/.azure:Z
    stdin_open: true
    tty: true
    command: ["/bin/bash"]

volumes:
  awscli-config:
    name: az-config
    driver: local
podman-compose run --rm az
podman-compose run --rm az login