Installing Ansible with Python

In posts Installing Ansible on Ubuntu 22.04 and Installing Ansible on Debian 12 Ansible was installed via the package manager. Another option is to install Ansible with Python in a virtual environment so you can have multiple Ansible versions installed on the same machine.

Using Python virtual environment

Since Ansible is written in Python, it can be installed in a Python virtual environment which is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. The virtual environment can be created with the venv module, which is part of the Python since version 3.4.

Let’s create a new directory for our Ansible project and create a virtual environment in it.

Create a new directory for Ansible project and create a virtual environment in it
$ mkdir ansible-project
$ cd ansible-project
$ python3 -m venv venv

Now we can activate the virtual environment and install Ansible with pip.

Start the virtual environment and install the latest Ansible version
$ source venv/bin/activate
$ pip install ansible

If we want to install a specific version of Ansible, we can do it like in the example below.

Start the virtual environment and install Ansible version 2.15.1
$ source venv/bin/activate
$ pip install ansible==2.15.1

After the installation is complete, we can check the Ansible version with the ansible --version command.

Start the virtual environment and install the latest Ansible version
$ ansible --version
ansible [core 2.15.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/sysadmin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/sysadmin/ansible-project/venv/lib/python3.11/site-packages/ansible
  ansible collection location = /home/sysadmin/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/sysadmin/ansible-project/venv/bin/ansible
  python version = 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] (/home/sysadmin/ansible-project/venv/bin/python)
  jinja version = 3.1.2
  libyaml = True

Using pipenv to manage virtual environment

Other option to manage virtual environment is to use pipenv. Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.

Install pipenv and Ansible
$ mkdir ansible-project
$ cd ansible-project
$ pipenv --python 3
$ pipenv install ansible

Ansible can be installed in a specific virtual environment with the --python option. In the example above, we installed Ansible in a virtual environment with Python version 3.11.2. After the installation is complete, we can check the Ansible version with the ansible --version command.

Start pipenv shell and check Ansible version
$ pipenv shell
$ ansible --version
ansible [core 2.15.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/sysadmin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/sysadmin/.local/share/virtualenvs/ansible-project-3Z1Z2Z2z/lib/python3.11/site-packages/ansible
  ansible collection location = /home/sysadmin/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/sysadmin/.local/share/virtualenvs/ansible-project-3Z1Z2Z2z/bin/ansible
  python version = 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] (/home/sysadmin/.local/share/virtualenvs/ansible-project-3Z1Z2Z2z/bin/python)
  jinja version = 3.1.2
  libyaml = True

Using poetry to manage virtual environment

Another option to manage virtual environment is to use poetry. Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

$ mkdir ansible-project
$ cd ansible-project
$ poetry init
$ poetry add ansible

With Poetry, we can install Ansible in a virtual environment with the poetry add command. After the installation is complete, we can check the Ansible version with the ansible --version command.

Start poetry shell and check Ansible version
$ poetry shell
$ ansible --version
ansible [core 2.15.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/sysadmin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/sysadmin/.cache/pypoetry/virtualenvs/ansible-project-3Z1Z2Z2z-py3.11/lib/python3.11/site-packages/ansible
  ansible collection location = /home/sysadmin/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/sysadmin/.cache/pypoetry/virtualenvs/ansible-project-3Z1Z2Z2z-py3.11/bin/ansible
  python version = 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] (/home/sysadmin/.cache/pypoetry/virtualenvs/ansible-project-3Z1Z2Z2z-py3.11/bin/python)
  jinja version = 3.1.2
  libyaml = True