Skip to content

Instantly share code, notes, and snippets.

@charbonnierg
Last active February 14, 2024 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charbonnierg/dabfe1a4ab3ea3d2b169053141689fed to your computer and use it in GitHub Desktop.
Save charbonnierg/dabfe1a4ab3ea3d2b169053141689fed to your computer and use it in GitHub Desktop.

Install azure-cli (ansible task file)

This gist describes how to install azure-cli with Ansible.

Disclaimer: Code has not been tested so it may not work, but the intent is still clear.

Reference: Azure/azure-cli#20476 (note that issue has been opened by an engineer working at Microsoft on Azure CLI, so I take it as "it's ok to install azure-cli with pip assumning you don't have problem with using Pypi".)

---
# This task file depends on the parameter "azure_cli_virtualenv"
#
# Once this task file is executed, it's possible to use azure cli
# using "{{ azure_cli_virtualenv }}/bin/az".
---
- name: Install azure-cli into dedicated virtual environment
# A block is used to group multiple tasks together
# NOTE 1: We could install azure-cli only in the virtual environment, but we would not be able
# to upgrade azure-cli manually later on if we choose to do so.
# NOTE 2: On Ubuntu >= 22.04, we can use the --upgrade-deps option to upgrade the pip/setuptools dependencies
# of the virtual environment. In such case, we would need a single task to both create the virtual environment
# and install the azure-cli dependency. However, on older versions of Ubuntu, we need to create the virtual
# environment first with up-to-date dependencies, then install the azure-cli dependency
block:
- name: Create a virtual environment for azure-cli
ansible.builtin.pip:
name: "{{ item }}"
virtualenv: "{{ azure_cli_virtualenv }}"
# Uncomment to support Ubuntu >= 22.04 only
# If you decide to uncomment the following line, you should also
# change the dependencies to install into [azure-cli] only in this
# task, and you should remove the next task.
# virtualenv_command : python3 -m venv --upgrade-deps
state: latest
# Rather than using --upgrade-deps, we can force the upgrade of the dependencies by using the following:
# Change the dependencies to install into [azure-cli] only in this task if you decide
# to use the virtualenv_comand ansible option with --upgrade-deps
loop:
- pip
- setuptools
- name: Install azure-cli into the virtual environment
ansible.builtin.pip:
name: azure-cli
virtualenv: "{{ azure_cli_virtualenv }}"
# This will not update the azure-cli package if it's already installed
# You may change the state to "latest" to force the upgrade of the package
# to the latest version on each run.
state: present
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment