Install Ansible

Install Ansible

In this article we’ll walk through installing Ansible on Debian 12 (Bookworm).

To take advantage of the most up-to-date version of Ansible, we’ll be installing Ansible via Python’s Package Manager (pip) rather than through the built-in apt package manager. This method is admittedly more complicated then using the built-in apt package manager but will allow us to install a more recent version of Ansible.

Directories Preparation

To begin, create a directory which will house our Python Virtual Environment. For this article, we’ll use /opt/virtualenv so its accessible to multiple users.

Install acl

Ensure the acl package is installed. Were using ACLs rather than the traditional unix permission as its more flexible when it comes to multi-user setups.

sudo apt install acl

Configure Group

Create a group to control access to the /opt/virtualenv directory. For this article we’ll use a group named: ansible.

sudo groupadd ansible

Add all user’s who will be administering Ansible into this group.

sudo usermod -a -G ansible username

Create Directory

Create the directory which will house our Python Virtual Environment.

sudo mkdir /opt/virtualenv

Configure Permissions

Configure the default permissions so the ansible group will have read, write and execute permissions on /opt/virtualenv.

sudo setfacl -m g:ansible:rwx /opt/virtualenv

Rerun the command, now with the -d flag (for default). This flag configures the default permissions for the directory so all underlying files/folders are given the same permissions when created.

sudo setfacl -d -m g:ansible:rwx /opt/virtualenv

Restart Shell

Restart the shell for the changes to take effect.

exec bash

Install Python

Now its time to create our Python Virtual Environment. Install the necessary packages.

sudo apt install python3-pip python3-venv

For folks wishing to manage both Linux and Windows hosts, install these additional packages.

sudo apt install python3-dev gcc libkrb5-dev krb5-user libkrb5-dev sshpass

During the installation, you’ll be asked to enter information regarding your realm. Feel free to skip this section as it will be covered later on.

Create Virtual Environment

Run the following command to create the Python Virtual Environment. The virtual environment will be stored in /opt/virtualenv.

python3 -m venv /opt/virtualenv

Activate Environment

Activate the virtual environment in preparation for installing Ansible.

source /opt/virtualenv/bin/activate

If you ever wish to leave the virtual environment, simply run deactivate.

deactivate

Alias (Optional)

Typing out source /opt/virtualenv/bin/activate to every time you wish to activate the Python Virtual Environment isn’t the most intuitive. To solve this, we can create an alias.

For this article, we’ll use the command activate as it pairs well with the built-in command to drop out of the virtual environment deactivate.

Throw the following to the bottom of your ~/.bashrc file to create the alias.

.bashrc
108
109
110
111
112
113
114
115
...
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

alias activate='source /opt/virtualenv/bin/activate'

Restart your shell to see the changes take effect.

exec bash
ℹ️
Add this chunk of code to the .bashrc file located under /etc/skel to have it automatically added to every new user of the system.

Now you can simply type activate to activate the virtual environment.

activate

To leave the virtual environment, enter deactivate.

deactivate

Install Ansible

With our virtual environment activated, install Ansible.

pip3 install ansible

If wishing to manage both Linux and Windows hosts, install these additional packages.

pip3 install requests pywinrm[kerberos]

Run ansible --version to confirm it installed successfully.

ansible [core 2.18.1]
  config file = None
  configured module search path = ['/home/tyler/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/virtualenv/lib/python3.11/site-packages/ansible
  ansible collection location = /home/tyler/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/virtualenv/bin/ansible
  python version = 3.11.2 (main, Sep 14 2024, 03:00:30) [GCC 12.2.0] (/opt/virtualenv/bin/python3)
  jinja version = 3.1.5
  libyaml = True

Ansible is fully installed on the host! Browsing through /opt/virtualenv you’ll see that Ansible is now installed within the virtual environment rather than the usual OS directories.