Build & Run Ansible Execution Environments Effectively
By Luca Berton · Published 2024-01-01 · Category: installation
Discover how to build and run Ansible Execution Environments using ansible-builder and ansible-runner tools.

How to build and Run an Ansible Execution Environment?
Using an Ansible Execution Environment is the latest technology to maintain up-to-date Python dependency of the Ansible collections without interfering with your Linux system. It's the evolution of Python Virtual Environment. This initial configuration sometimes is a roadblock for some Ansible users.See also: Build a Custom Ansible Execution Environment Easily
Ansible Execution Environment
ansible-builderansible-runner
Containerfile, along with any other files that need to be added to the image.
The execution is performed by the Ansible Runner tool.
The Ansible Runner enables you to run the Execution Environment as a container in the current machine. It is basically taking care that the content runs as expected.
Links
- https://www.redhat.com/en/technologies/management/ansibleproducts/execution-environments
- https://www.redhat.com/en/technologies/management/ansible/automation-execution-environments
- https://docs.ansible.com/automation-controller/latest/html/userguide/execution_environments.html
Playbook
How to Build and Run a custom-built "my_ee" Ansible Execution Environment using ansible-builder and ansible-runner tools.Demo Build Ansible Execution Environment
How to Build an Ansible Execution Environment using the ansible-builder tool.
- name: my_ee
- System (git)
- Python (boto3)
- Collection (community.aws)
git, Python libraries boto3 and Amazon Collection community.aws dependencies.
code
- execution-environment.yml
---
version: 1
dependencies:
galaxy: requirements.yml
python: requirements.txt
system: bindep.txt
additional_build_steps:
prepend: |
RUN pip3 install --upgrade pip setuptools
append:
- RUN ls -al /- requirements.yml
---
collections:
- name: community.aws
requirements.txt
botocore>=1.18.0
boto3>=1.15.0
boto>=2.49.0- bindep.txt
git [platform:rpm]
git [platform:dpkg]execution
$ ansible-builder build -t my_ee -v 3
Running command:
podman build -f context/Containerfile -t my_ee context
Complete! The build context can be found at: /home/devops/ee/context[root@demo ee]# dnf install ansible-builder$ podman login registry.redhat.ioA successful build produces the following context/Containerfile:
ARG EE_BASE_IMAGE=registry.redhat.io/ansible-automation-platform-22/ee-minimal-rhel8:latest
ARG EE_BUILDER_IMAGE=registry.redhat.io/ansible-automation-platform-22/ansible-builder-rhel8:latest
FROM $EE_BASE_IMAGE as galaxy
ARG ANSIBLE_GALAXY_CLI_COLLECTION_OPTS=
USER root
ADD _build /build
WORKDIR /build
RUN ansible-galaxy role install -r requirements.yml --roles-path "/usr/share/ansible/roles"
RUN ANSIBLE_GALAXY_DISABLE_GPG_VERIFY=1 ansible-galaxy collection install $ANSIBLE_GALAXY_CLI_COLLECTION_OPTS -r requirements.yml --collections-path "/usr/share/ansible/collections"
FROM $EE_BUILDER_IMAGE as builder
COPY --from=galaxy /usr/share/ansible /usr/share/ansible
ADD _build/requirements.txt requirements.txt
ADD _build/bindep.txt bindep.txt
RUN ansible-builder introspect --sanitize --user-pip=requirements.txt --user-bindep=bindep.txt --write-bindep=/tmp/src/bindep.txt --write-pip=/tmp/src/requirements.txt
RUN assemble
FROM $EE_BASE_IMAGE
USER root
RUN pip3 install --upgrade pip setuptools
COPY --from=galaxy /usr/share/ansible /usr/share/ansible
COPY --from=builder /output/ /output/
RUN /output/install-from-bindep && rm -rf /output/wheels
RUN ls -la /Demo Run Ansible Execution Environment
How to Run an Ansible Execution Environment using ansible-runner tool.
code
- ping.yml
---
- name: ping module Playbook
hosts: all
tasks:
- name: test connection
ansible.builtin.ping:- inventory
localhost ansible_connection=localexecution
[devops@demo ee]$ ansible-runner run -p ping.yml --inventory inventory --container-image=my_ee .
PLAY [ping module Playbook] ********************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [test connection] *********************************************************
ok: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[devops@demo ee]$When the ansible-runner tool is not installed you should install it via DNF command using the ansible Automation Platform subscription::
[root@demo ee]# dnf install ansible-runnerSee also: Run an Ansible Execution Environment - ansible-runner command-line tool
Conclusion
Now you know how to Build and Run an Ansible Execution Environment using ansible-builder and ansible-runner tools.
Related Articles
Category: installation
Watch the video: Build & Run Ansible Execution Environments Effectively — Video Tutorial