Ansible is a powerful automation and configuration management tool widely used in the DevOps world. One of the recent additions to Ansible is Execution Environments, which simplifies the management of Ansible collections and dependencies. In this blog post, we will explore what Ansible Execution Environments are and provide a quick start guide to help you get started.

Don’t forget to join my Discord: https://discord.gg/YbSYGsQYES
What Are Ansible Execution Environments?
Ansible Execution Environments are a feature introduced in Ansible 2.10 and later versions. They offer a more structured and reliable way to manage collections and their dependencies. Traditionally, managing Ansible collections and their versions could be a bit challenging, but Execution Environments aim to streamline this process.
Execution Environments provide an isolated, self-contained environment that includes Ansible and the collections required to run your playbooks. This means that you can specify the exact versions of collections you want to use for your automation tasks, ensuring consistency and predictability in your workflows.
Quick Start with Ansible Execution Environments
Let’s dive into a step-by-step guide to get you started with Ansible Execution Environments.
1. Install Ansible
If you haven’t already, you need to install Ansible on your system. You can use Python’s package manager, pip, to install Ansible:
pip install ansible
2. Create a New Execution Environment
You can create an Execution Environment using the ansible-galaxy command. For example, to create an Execution Environment named “myenv,” run:
ansible-galaxy collection install -r requirements.yml -p environments/myenv
In this command:
-r requirements.ymlspecifies a file containing the list of collections and their versions you want to include in the Execution Environment.-p environments/myenvspecifies the target directory where the Execution Environment will be created.
3. Create a requirements.yml File
To define the collections and their versions you want to include in your Execution Environment, create a requirements.yml file. Here’s an example:
---
collections:
- name: ansible.builtin
version: 1.0.0
- name: community.general
version: 3.0.0
In this file, you list the collections and their desired versions. Update this file to include the collections you need for your specific project.
4. Activate the Execution Environment
To activate your newly created Execution Environment, use the ansible-config command:
ansible-config environment -a environments/myenv
This sets the active Execution Environment for your Ansible session.
5. Run Playbooks with Your Execution Environment
Now that you’ve created and activated your Execution Environment, you can run Ansible playbooks using the collections and versions specified in your environment. For example:
ansible-playbook -i inventory.ini myplaybook.yml
Your playbook will run with the dependencies defined in your Execution Environment, ensuring a consistent and reproducible execution.
6. Deactivate the Execution Environment
To switch back to the system-level environment, you can deactivate the Execution Environment:
ansible-config environment --remove
Conclusion
Ansible Execution Environments provide a more structured and reliable way to manage collections and their dependencies. By creating isolated environments with specific collections and versions, you can ensure consistency and predictability in your automation workflows. With this quick start guide, you can begin using Execution Environments to streamline your Ansible automation tasks and simplify your DevOps operations.
Don’t forget to join my Discord: https://discord.gg/YbSYGsQYES