Here is how to install Ansible in your HOME dir using virtual environments. This version is limited per user in their HOME dir and separate from the system installed Ansible and modules. There won’t be any conflict and no need to remove system install Ansible and co.
Step 1: Install the tool to create isolated Python environments
First, install Python virtualenv using dnf command or apt command (as per your distro). For example:
apt install python3-virtualenv
sudo apt install python3-virtualenv python3-pip # Debian/Ubuntu
sudo dnf install python3-virtualenv python3-pip # Rocky/ALma/RHEL/Fedora/CentOS Linux
The above will also install pip to install Ansible.
Step 2: Creating a virtual environment to host a local copy of Ansible in your $HOME
cd # go to home dir
virtualenv ansible-aws-project
This will create a new dir called ansible-aws-project
in the current directory. To use ansible-aws-project dir, you must activate it. For example:
source ~/ansible-aws-project/bin/activate
You will see the shell prompt as follows:
(ansible-aws-project) vivek@desktop:~$
Step 3: Installing Ansible
Next, install Ansible to make it possible to use the modules with the latest version. Here is how to do it:
pip install Ansible
## OR ##
pip3 install Ansible
Check your Ansible version, and type:
ansible --version
ansible [core 2.13.8]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/vivek/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/vivek/ansible-aws-project/lib/python3.8/site-packages/ansible
ansible collection location = /home/vivek/.ansible/collections:/usr/share/ansible/collections
executable location = /home/vivek/ansible-aws-project/bin/ansible
python version = 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]
jinja version = 3.1.2
libyaml = True
Step 4: Test it
That is all. It should work now without any warnings. For example:
ansible-playbook -i ~/project_app1/hosts playbook.yaml
You must activate virtual environment before getting started with Ansible. For instance:
source ~/ansible-aws-project/bin/activate
## now do your stuff
To exit from virtual environment, run:
exit
Step 5: Upgrading Ansible
When you need to upgrade Ansible, you can run the following commands:
source ~/ansible-aws-project/bin/activate
pip install --upgrade ansible
## OR ##
pip3 install --upgrade ansible