Ansible with DellEMC Storage: Part 8 – Manage Python packages inside Ansible AWX/Tower

Python has a great package manager called “pip”. I guess if you have come to this page you don’t need many introductions. As we know it is a good practice to deploy Python applications in their own virtual environments. When running a virtual environment we typically have to activate it before we can use “pip” to manage packages

Ansible AWX/Tower is written in Python and it also runs on its own virtual environment, so you need to activate it before start using pip. However many small deployments like mine are done inside a single virtual machine by using Docker, which requires an extra step

As an example problem let’s look at an issue I encountered when installing a playbook to automate the configuration of Brocade switches. The AWX template that I created was failing with an error about Python’s “enum” package.

File \"/usr/lib64/python3.6/\", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'"

After a bit of research I found that uninstalling the “enum” package might fix the problem. In a previous article we explained how to install Ansible modules inside the AWX. In this case we will also start by opening a terminal session into the “awx_task” container.

[root@alb-awx awx]# docker ps
CONTAINER ID        IMAGE                        NAMES
a51aadbd6554        ansible/awx_task:7.0.0       awx_task
7c0092e76d9c        ansible/awx_web:7.0.0        awx_web
fdf972150850        memcached:alpine             awx_memcached
e24ca64c33bd        ansible/awx_rabbitmq:3.7.4   awx_rabbitmq
f855fcbcd05a        postgres:10                  awx_postgres

[root@alb-awx awx]# docker exec -it awx_task /bin/bash

This is the container that executes your playbooks. So now to manage Python packages with pip we need to activate the virtual environment

bash-4.2# source /var/lib/awx/venv/ansible/bin/activate
(ansible) bash-4.2#

Notice how the prompt now has the “(ansible)” prefix to indicate that the virtual environment is active. Now we can use pip for whatever we need. In my case I uninstalled “enum”

(ansible) bash-4.2# pip uninstall enum34

Once you are here you can troubleshoot things better because you get the full “awx_tower” point of view. For example my playbook runs a “shell” task with a Python script. I can try to run the script now to see if the issue is gone once the package is no longer there

The “awx_task” container stores your project files in the “/var/lib/awx/projects” directory

(ansible) bash-4.2# ls /var/lib/awx/projects
_15__hello_world         _19__hello_world         _24__hello_vm       _52__brocade_test
_15__hello_world.lock    _19__hello_world.lock    _24__hello_vm.lock  _52__brocade_test.lock

Let’s go to my project directory

(ansible) bash-4.2# cd /var/lib/awx/projects/_52__brocade_test/

I attempt to run the Python script and it behaves as expected

(ansible) bash-4.2# python3 spec1 oracle01 PowerStore finance
{"aliases": [{"name": "oracle01_port1", "members": ["21:00:00:24:ff:16:de:c2"]}], ...

The only thing is left to do now is to exit the Python virtual environment and the terminal session to the “awx_task” container

(ansible) bash-4.2# deactivate
bash-4.2# exit
[root@alb-awx awx]#

Hope it helps!

1 reply »

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s