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.

2 thoughts on “Custom Ansible Execution Environments creation

  1. Andraz

    Hi, great article!

    I am just wondering why you chose to compile python3.10 yourself. Sadly it is missing as a yumpackage in Centos Stream 9, but you can easily use 3rd party manager like mise, that just download compiled binary.

    # Install mise
    curl https://mise.run | sh
    # Install python to specific location
    mise install-into python@3.10.18 /opt/python3.10
    # Create symlink, so will you be able to execute it easier
    ln -s /opt/python3.10/bin/python3.10 /usr/bin/python3.10
    # Test time!
    python3.10 –version

    Keep up writing!

    1. Anonymous

      Hi,
      I had been looking for a way to add a specific Python version to the execution environment and wasn’t aware that tools like mise or asdf existed for managing development tools. In the past, I hadn’t needed such tools, so I solved it the way I knew how at the time.
      Since you introduced me to mise, I went back to the drawing board and was able to simplify the entire workflow for my use case. I no longer need to create a custom execution environment, as I can now install and configure everything with one additional Ansible task — to install and activate mise, and install the required Python version using mise.
      Thank you for the helpful comment and new insight — I really appreciate it!
      Best regards,
      Jan

Leave a Reply to Anonymous Cancel reply