Ansible is distributed as a Python module installable via PIP. The installation process is straightforward but warrants some considerations.
Typically, the automation host, the system from which all automation scripts originate, is one of the most critical systems within an infrastructure. It can access all systems that need to be automated and often, for convenience, contains the credentials for these devices. While credentials are stored in specific password managers, more often than not, they are found in plaintext.
For simplicity and clarity, the simplest playbooks contain device credentials. They are designed to focus on the automation aspect rather than the security of the system, which will be discussed in specific articles.
However, we can start with a best practice: creating a dedicated Python environment for Ansible and tracking the versions of installed packages. Ansible, like many other software, is continuously evolving, and playbooks written today may not function correctly with future versions.
The version of Ansible we will use requires Python 3.9, which is not installable via APT on Ubuntu Linux 20. Therefore, we need to install it manually from sources:
cd /usr/src
wget https://www.python.org/ftp/python/3.9.16/Python-3.10.8.tgz
tar xzf Python-3.10.8.tgz
cd Python-3.10.8/
./configure --enable-optimizations
make -j 4
sudo make altinstall
The updated packages.txt
file is available in the , which can be used to install the necessary dependencies for all available labs.
sudo apt-get install -y < packages.txt
In general, it’s preferable to have a dedicated user for Ansible usage to ensure its environment is configured and reliable. Let’s create a new user:
sudo useradd -m -d /opt/ansible -s /bin/bash ansible
sudo su - ansible
Now, we can prepare the Python environment and install Ansible:
python3.10 -m venv .venv
source .venv/bin/activate
pip install ansible ansible-pylibssh
pip freeze | grep ansible
ansible --version
The updated requirement.txt
file is available in the , which can be used to install the necessary dependencies for all available labs.
Remember to activate the newly created Python environment before running Ansible:
source .venv/bin/activate
You can deactivate the environment using the following command:
Since we’re using a dedicated user, we can load the virtual environment directly at login by adding the following command to .bashrc
:
source .venv/bin/activate