Execution Environments Demystified: Build Once, Run Anywhere, Pin Every Version
Building Your First Custom EE
Suppose we want to have a number of playbooks that use Python psutil to monitor system resources. These playbooks use a specific version of Ansible (2.15.0), a specific set of collections (community.general and ansible.posix), and a specific version of psutil (5.9.8). We can create a custom execution environment that includes all the necessary components and use it to run our playbooks.
To build your own environment, you can use the ansible-builder tool. Let's see an example. Start by installing a virtual environment and the necessary packages. We are going to use awx-1 to execute the following commands:
# Create a folder for the environment
cd $HOME
mkdir -p execution-environments/custom
cd execution-environments/custom
# Update the package list
yum check-update
# Install Python 3 and pip
yum install -y python3 python3-pip
# Create a virtual environment
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate
# Install ansible-builder
pip install ansible-builder==3.1.1
You need to install Docker or Podman to build the container image. For this example, we'll use Docker:
# Install Docker if you didn't already
curl -fsSL https://get.docker.com -o get-docker.sh
bash get-docker.sh --version 29.4.3
# Start and enable Docker
systemctl start docker
systemctl enable docker
To define a new environment, we need to create an execution-environment.yml file. In this file, we define:
- version: The version of the execution environment file.
- images and base_image: The base image to use for the environment.
- dependencies: The dependencies to install in the environment.
Here is the content of the execution-environment.yml file:
cat < execution-environment.yml
---
version: 3
images:
base_image:
name: quay.io/centos/centos:stream9
dependencies:
ansible_core:
package_pip: ansible-core==2.15.0
ansible_runner:
package_pip: ansible-runner
galaxy: requirements.yml
python: requirements.txt
system: bindep.txt
EOF
In this example, we use quay.io/centos/centos:stream9 as the base and install ansible-core 2.15. We also install dependencies from a requirements.yml file and a requirements.txt file. The requirements.yml file installs Ansible collections, the requirements.txt file installs Python packages, and the bindep.txt file installs system-level dependencies.
Next, create a requirements.yml file with the following content:
AWX in Action
Ansible Orchestration at ScaleEnroll now to unlock all content and receive all future updates for free.
