Automation
jkovic  

Custom Ansible Execution Environments creation

I have encountered a problem where I needed to have multiple python versions and python libraries installed in a single execution environment to use with ansible.

In this article I will be using ansible builder to create a custom version of the latest awx-ee image with an alternative install of python 3.10.13.

Building an Execution Environment

Directory creation

$ mkdir custom_ee
$ cd custom_ee

Define custom execution evironment files

bindep.txt
The bindep.txtis a file that installs extra system-level dependencies that are outside of what the collections already include as their dependencies.

$ vim bindep.txt
libxml2-devel [platform:rpm]
subversion [platform:rpm]

requirements.txt
The requirements.txt is a file that installs extra Python requirements on top of what the Collections already list as their Python dependencies.

$ vim requirements.txt
six
psutils

execution-environment.yml
The execution-environment.yml is a file that specifies which content you want to include in your execution environment, such as collections, Python requirements, and system-level packages.

$ vim execution-environment.yml
version: 3

dependencies: 
  python: requirements.txt
  system: bindep.txt

options:
  package_manager_path: /usr/bin/dnf

images: 
  base_image:
    name: quay.io/ansible/awx-ee:latest

additional_build_steps:
  #  Injects steps before the base image is defined.
  #prepend_base:

  # Injects steps after the base image is defined but before dependencies are installed.
  #append_base:

  #  Injects steps before the final image stage begins.
  #prepend_final:

  # Injects steps after the final image stage is built.
  append_final:
    - RUN python -m pip install --upgrade pip
    - RUN curl -O https://www.python.org/ftp/python/3.10.13/Python-3.10.13.tgz
    - RUN tar -xzf Python-3.10.13.tgz
    - RUN cd Python-3.10.13 && ./configure --enable-optimizations --prefix=/opt/python3.10
    - RUN cd Python-3.10.13 && make -j$(nproc)
    - RUN cd Python-3.10.13 && make altinstall
    - RUN echo "Installed Python 3.10 with altinstall"
    - RUN /opt/python3.10/bin/python3.10 --version

Install Ansible Builder

$ pip3 install ansible-builder

Build the Execution Environment

$ ansible-builder build -f execution-environment.yml -t builder_ee_image_v1 -v3

Check image with Podman

$ podman images
REPOSITORY                     TAG    IMAGE ID      CREATED      SIZE
localhost/builder_ee_image_v1  latest 26a8361b21db  4 weeks ago  4.21 GB

Run image with Podman

# command to run container interactively
$ podman run -it imageID /bin/bash

# running the container with the image called: localhost/builder_ee_image_v1 and imageID: 26a8361b21db.
$ podman run -it 26a8361b21db /bin/bash
bash-5.1$

# Check python version
bash-5.1$ python --version
Python 3.11.11

# Check python3 version
bash-5.1$ python3 --version
Python 3.9.23

# Check custom python version
bash-5.1$ /opt/python3.10/bin/python3.10 --version
Python 3.10.13

# Exit container
bash-5.1$ exit

Check running containers

$ podman ps -a
CONTAINER ID  IMAGE                                 COMMAND    CREATED        STATUS                     PORTS           NAMES
6098e734ccaf  localhost/builder_ee_image_v1:latest  /bin/bash  4 minutes ago  Exited (0) 17 seconds ago  cool_murdock

Final thought

In this article we have created a custom Execution Environment based on the latest awx-ee image. we can use in with ansible that has installed three versions of python:
– Python 3.10.13 – which I need for my python script – /opt/python3.10/bin/python3.10
– Python 3.11.11 – default install – python
– Python 3.9.23 – default install – python3

By using the files bindep.txt and requirements.txt you can further customize the execution environemnt based on your needs.

I hope this article was helpful. Thank you for reading this post and feel free to leave a comment.

Leave A Comment