Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lwillek/6a34c81295e8daa0e2ebd00c4618e54e to your computer and use it in GitHub Desktop.
Save lwillek/6a34c81295e8daa0e2ebd00c4618e54e to your computer and use it in GitHub Desktop.
Setup Ansible dynamic inventories usable in Azure

Install Python and virtual environment with help of pyenv

Install OS-specific dependencies

Before you install pyenv itself, you’re going to need some OS-specific dependencies. Pick whatever suits you.

Linux (Debian/Ubuntu)

$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

Linux (Fedora/RHEL/EntOS/Alpine)

$ sudo yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel git

Linux (OpenSuse)

$ zypper in zlib-devel bzip2 libbz2-devel libffi-devel \
libopenssl-devel readline-devel sqlite3 sqlite3-devel xz xz-devel git

Linux (Alpine)

$ apk add libffi-dev ncurses-dev openssl-dev readline-dev \
tk-dev xz-dev zlib-dev git

MacOS

$ brew install openssl readline sqlite3 xz zlib git

Installation of pyenv and pyenv-virtualenv

Install pyenv

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ cd ~/.pyenv && src/configure && make -C src ; cd

Install pyenv-virtualenv

pyenv-virtualenv is a pyenv plugin that provides features to manage virtualenvs.

$ export PATH="$HOME/.pyenv/bin:$PATH"
$ eval "$(pyenv init -)"
$ git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

Configure your environment to use pyenv

Load pyenv automatically by adding the following to your standard shell rc file, such as ~/.bashrc or ~/.zshrc.

export PYENV_ROOT="$HOME/.pyenv"
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Activate it.

$ exec "$SHELL" # Or just restart your terminal

Install some Python versions

This will take a while because pyenv is building Python from source. Here in this example, all the available 3.X versions are listed, then three different versions are installed. Finally all installed versions are listed. Pick whatever version suits you.

$ pyenv install --list | grep " 3\."
$ pyenv install -v 3.8.17 3.10.12 3.11.4
$ pyenv versions

Configure some sensible environment defaults

Pick whatever version suits you. This is made for Azure, so in this example the azure-cli is installed within the default venv.

$ pyenv virtualenv 3.11.4 default
$ pyenv global system
$ cd ~ && pyenv local default
$ pyenv shell default
$ pip install --upgrade pip
$ pip install azure-cli
$ az login --output table

Setup Ansible for use with Azure and dynamic inventories

This will be installed within its own venv.

$ pyenv virtualenv 3.10.12 ansible
$ pyenv shell ansible
$ pip install --upgrade pip wheel
$ pip install ansible[azure] ansible-lint msrest msrestazure azure-cli

Configure your git repos

This tutorial assumes there are 2 git repositories ansible-configuration-management and ansible-vault. Adapt as needed.

$ cd ~/git/github.com/PATH/TO/ansible-vault ; pyenv local ansible
$ cd ~/git/github.com/PATH/TO/ansible-configuration-management ; pyenv local ansible

Testing

$ cd ~/git/github.com/PATH/TO/ansible-configuration-management
$ az login --output table
$ az account set -s MY-SUBSCRIPTION
$ ansible-inventory --graph -i inventories/PATH/TO/azure_rm.yml

pip freeze

adal==1.2.7
ansible==8.2.0
ansible-compat==4.1.5
ansible-core==2.15.2
ansible-lint==6.17.2
antlr4-python3-runtime==4.9.3
applicationinsights==0.11.10
argcomplete==3.1.1
attrs==23.1.0
azure-appconfiguration==1.1.1
azure-batch==13.0.0
azure-cli==2.50.0
azure-cli-core==2.50.0
azure-cli-telemetry==1.0.8
azure-common==1.1.28
azure-core==1.28.0
azure-cosmos==3.2.0
azure-data-tables==12.4.0
azure-datalake-store==0.0.53
azure-graphrbac==0.60.0
azure-keyvault==1.1.0
azure-keyvault-administration==4.3.0
azure-keyvault-certificates==4.7.0
azure-keyvault-keys==4.8.0b2
azure-keyvault-secrets==4.7.0
azure-loganalytics==0.1.1
azure-mgmt-advisor==9.0.0
azure-mgmt-apimanagement==4.0.0
azure-mgmt-appconfiguration==3.0.0
azure-mgmt-appcontainers==2.0.0
azure-mgmt-applicationinsights==1.0.0
azure-mgmt-authorization==3.0.0
azure-mgmt-batch==17.0.0
azure-mgmt-batchai==7.0.0b1
azure-mgmt-billing==6.0.0
azure-mgmt-botservice==2.0.0
azure-mgmt-cdn==12.0.0
azure-mgmt-cognitiveservices==13.3.0
azure-mgmt-compute==29.1.0
azure-mgmt-consumption==2.0.0
azure-mgmt-containerinstance==10.1.0
azure-mgmt-containerregistry==10.1.0
azure-mgmt-containerservice==24.0.0
azure-mgmt-core==1.4.0
azure-mgmt-cosmosdb==9.2.0
azure-mgmt-databoxedge==1.0.0
azure-mgmt-datalake-analytics==0.2.1
azure-mgmt-datalake-nspkg==3.0.1
azure-mgmt-datalake-store==0.5.0
azure-mgmt-datamigration==10.0.0
azure-mgmt-devtestlabs==4.0.0
azure-mgmt-dns==8.0.0
azure-mgmt-eventgrid==10.2.0b2
azure-mgmt-eventhub==10.1.0
azure-mgmt-extendedlocation==1.0.0b2
azure-mgmt-hdinsight==9.0.0
azure-mgmt-imagebuilder==1.2.0
azure-mgmt-iotcentral==10.0.0b2
azure-mgmt-iothub==2.3.0
azure-mgmt-iothubprovisioningservices==1.1.0
azure-mgmt-keyvault==10.2.2
azure-mgmt-kusto==0.3.0
azure-mgmt-loganalytics==13.0.0b4
azure-mgmt-managedservices==1.0.0
azure-mgmt-managementgroups==1.0.0
azure-mgmt-maps==2.0.0
azure-mgmt-marketplaceordering==1.1.0
azure-mgmt-media==9.0.0
azure-mgmt-monitor==5.0.1
azure-mgmt-msi==7.0.0
azure-mgmt-netapp==10.0.0
azure-mgmt-nspkg==3.0.2
azure-mgmt-policyinsights==1.1.0b4
azure-mgmt-privatedns==1.0.0
azure-mgmt-rdbms==10.2.0b10
azure-mgmt-recoveryservices==2.4.0
azure-mgmt-recoveryservicesbackup==6.0.0
azure-mgmt-redhatopenshift==1.2.0
azure-mgmt-redis==14.1.0
azure-mgmt-relay==0.1.0
azure-mgmt-resource==23.1.0b2
azure-mgmt-search==9.0.0
azure-mgmt-security==3.0.0
azure-mgmt-servicebus==8.2.0
azure-mgmt-servicefabric==1.0.0
azure-mgmt-servicefabricmanagedclusters==1.0.0
azure-mgmt-servicelinker==1.2.0b1
azure-mgmt-signalr==1.1.0
azure-mgmt-sql==4.0.0b10
azure-mgmt-sqlvirtualmachine==1.0.0b5
azure-mgmt-storage==21.0.0
azure-mgmt-synapse==2.1.0b5
azure-mgmt-trafficmanager==1.0.0
azure-mgmt-web==7.0.0
azure-multiapi-storage==1.2.0
azure-nspkg==3.0.2
azure-storage-common==1.4.2
azure-synapse-accesscontrol==0.5.0
azure-synapse-artifacts==0.15.0
azure-synapse-managedprivateendpoints==0.4.0
azure-synapse-spark==0.2.0
bcrypt==4.0.1
black==23.7.0
bracex==2.3.post1
certifi==2023.7.22
cffi==1.15.1
chardet==3.0.4
charset-normalizer==3.2.0
click==8.1.6
colorama==0.4.6
cryptography==41.0.2
Deprecated==1.2.14
fabric==2.7.1
filelock==3.12.2
humanfriendly==10.0
idna==3.4
invoke==1.7.3
isodate==0.6.1
javaproperties==0.5.2
Jinja2==3.1.2
jmespath==1.0.1
jsondiff==2.0.0
jsonschema==4.18.4
jsonschema-specifications==2023.7.1
knack==0.10.1
markdown-it-py==3.0.0
MarkupSafe==2.1.3
mdurl==0.1.2
msal==1.22.0
msal-extensions==1.0.0
msrest==0.7.1
msrestazure==0.6.4
mypy-extensions==1.0.0
oauthlib==3.2.2
packaging==23.1
paramiko==3.2.0
pathlib2==2.3.7.post1
pathspec==0.11.1
pkginfo==1.9.6
platformdirs==3.9.1
portalocker==2.7.0
psutil==5.9.5
pycparser==2.21
PyGithub==1.59.0
Pygments==2.15.1
PyJWT==2.8.0
PyNaCl==1.5.0
pyOpenSSL==23.2.0
PySocks==1.7.1
python-dateutil==2.8.2
PyYAML==6.0.1
referencing==0.30.0
requests==2.31.0
requests-oauthlib==1.3.1
resolvelib==1.0.1
rich==13.4.2
rpds-py==0.9.2
ruamel.yaml==0.17.32
ruamel.yaml.clib==0.2.7
scp==0.13.6
semver==2.13.0
six==1.16.0
sshtunnel==0.1.5
subprocess-tee==0.4.1
tabulate==0.9.0
tomli==2.0.1
typing_extensions==4.7.1
urllib3==2.0.4
wcmatch==8.4.1
websocket-client==1.3.3
wrapt==1.15.0
xmltodict==0.13.0
yamllint==1.32.0

Further Reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment