How to configure a Python Virtual Environment for Ansible AWS?
Using a Python Virtual Environment is a convenient way to maintain up-to-date Python dependency of the Ansible collection amazon.aws without interfering with your Linux system.
This initial configuration sometimes is a roadblock for some AWS users to start using Ansible.
I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Links

Playbook
Configure a Python Virtual Environment for Ansible AWS:
- boto3
How to Python Virtual Environment for Ansible AWS.
I’m going to show you how to configure a Python Virtual Environment for Ansible AWS to successfully use the Ansible collection amazon.aws
of modules and plugins to manage various operations related to AWS infrastructure such as EC2, VPC, Security Groups, etc.
Ansible AWS modules are written on top of boto3
. Boto3 is the Python SDK for the AWS that allows users to manage AWS infrastructure: EC2, VPC, Security Groups, etc.
code
$ python3.8 -m venv venv
$ source venv/bin/activate
(venv) $ pip3.8 install --upgrade pip
(venv) $ pip3.8 install boto3
(venv) $ ansible-galaxy collection install amazon.aws
execution
$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo ~]$ whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python.1.gz
[devops@demo ~]$ python3.8 -m venv venv
[devops@demo ~]$ source venv/bin/activate
(venv) [devops@demo ~]$ pip3.8 install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/96/2f/caec18213f6a67852f6997fb0673ae08d2e93d1b81573edb93ba4ef06970/pip-22.1.2-py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.3.1
Uninstalling pip-19.3.1:
Successfully uninstalled pip-19.3.1
Successfully installed pip-22.1.2
(venv) [devops@demo ~]$ pip3.8 install boto3
Collecting boto3
Downloading boto3-1.24.27-py3-none-any.whl (132 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 132.5/132.5 kB 392.0 kB/s eta 0:00:00
Collecting botocore<1.28.0,>=1.27.27
Downloading botocore-1.27.27-py3-none-any.whl (9.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.0/9.0 MB 2.7 MB/s eta 0:00:00
Collecting jmespath<2.0.0,>=0.7.1
Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)
Collecting s3transfer<0.7.0,>=0.6.0
Downloading s3transfer-0.6.0-py3-none-any.whl (79 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 79.6/79.6 kB 3.3 MB/s eta 0:00:00
Collecting python-dateutil<3.0.0,>=2.1
Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 kB 4.0 MB/s eta 0:00:00
Collecting urllib3<1.27,>=1.25.4
Downloading urllib3-1.26.10-py2.py3-none-any.whl (139 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 139.2/139.2 kB 3.8 MB/s eta 0:00:00
Collecting six>=1.5
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: urllib3, six, jmespath, python-dateutil, botocore, s3transfer, boto3
Successfully installed boto3-1.24.27 botocore-1.27.27 jmespath-1.0.1 python-dateutil-2.8.2 s3transfer-0.6.0 six-1.16.0 urllib3-1.26.10
(venv) [devops@demo ~]$ pip3.8 list | grep boto
boto3 1.24.27
botocore 1.27.27
(venv) [devops@demo ~]$ pip3.8 freeze > requirements.txt
(venv) [devops@demo ~]$ deactivate
[devops@demo ~]$ source venv/bin/activate
(venv) [devops@demo ~]$ pip list
Package Version
--------------- -------
boto3 1.24.27
botocore 1.27.27
jmespath 1.0.1
pip 22.1.2
python-dateutil 2.8.2
s3transfer 0.6.0
setuptools 41.6.0
six 1.16.0
urllib3 1.26.10
(venv) [devops@demo ~]$ ansible-galaxy collection install amazon.aws
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/download/amazon-aws-4.0.0.tar.gz to /home/devops/.ansible/tmp/ansible-local-5083_hekoyln/tmpawwwduwa/amazon-aws-4.0.0-_wa4znqr
Installing 'amazon.aws:4.0.0' to '/home/devops/.ansible/collections/ansible_collections/amazon/aws'
amazon.aws:4.0.0 was installed successfully
(venv) [devops@demo ~]$ ansible-galaxy collection list amazon.aws
# /usr/lib/python3.8/site-packages/ansible_collections
Collection Version
---------- -------
amazon.aws 2.1.0
# /home/devops/.ansible/collections/ansible_collections
Collection Version
---------- -------
amazon.aws 4.0.0
(venv) [devops@demo ~]$
- requirements.txt
boto3==1.24.27
botocore==1.27.27
jmespath==1.0.1
python-dateutil==2.8.2
s3transfer==0.6.0
six==1.16.0
urllib3==1.26.10
Conclusion
Now you know how to Configure a Python Virtual Environment for Ansible AWS. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate