As the DevOps landscape evolves, so do our methods for managing tools like Ansible. This post outlines my current preferred approach to installing and managing Ansible on macOS.
The Traditional Homebrew Approach
For years, many macOS users, including myself, relied on Homebrew to install and maintain Ansible. The process was straightforward:
brew install ansible
While this method effectively managed the core Ansible installation, it became problematic as Ansible’s ecosystem grew more complex.
The Dependency Dilemma
As more collections moved away from the core Ansible distribution, managing prerequisites for various collections became increasingly challenging. Some collections had specific dependency requirements, leading to potential conflicts and a maintenance headache - especially when doing other Python development which have the same dependency requirements but you need to use other versions than those defined by the requirements.txt
which ships with a collection.
A Modern Solution: Conda for Ansible Management
Having experienced the pitfalls of Python dependency management, I’ve transitioned to using Conda for Ansible. This approach allows for a dedicated, isolated Ansible environment, mitigating the risks of “dependency hell.”
Step-by-Step Installation Process
- Create and activate a Conda environment for Ansible:
conda create -n ansible python=3.12
conda activate ansible
- Install Ansible within the environment:
python -m pip install ansible
Managing Ansible Collections
Once Ansible is installed, you can add collections and their dependencies using Ansible Galaxy and pip.
Tip
Always ensure you’re in your Ansible Conda environment by running conda activate ansible
before executing the following commands.
For example, to install the Azure Collection and its dependencies:
ansible-galaxy collection install azure.azcollection
python -m pip install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt
Upgrading Ansible and Collections
Keeping Ansible and its collections up-to-date is crucial. Here’s how to do it:
- Upgrade Ansible:
python -m pip install ansible --upgrade
- Upgrade a specific collection (e.g., Azure):
ansible-galaxy collection install azure.azcollection --upgrade
python -m pip install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt
Summary and Best Practices
Adopting Conda for Ansible management on macOS offers several advantages:
- Isolation: Your Ansible setup exists in its own environment, preventing conflicts with other Python applications.
- Flexibility: Easily manage different versions of Ansible for various projects.
- Dependency Control: Better handle complex dependencies required by different Ansible collections.
- Consistency: Ensure a consistent environment across different machines or team members.
To make the most of this setup:
- Regularly update both Ansible and its collections to benefit from the latest features and security patches.
- Explore Conda’s environment export features to share your exact setup with team members.
By embracing this modern approach to Ansible management on macOS, you’ll find yourself with a more maintainable, flexible, and robust Ansible environment. Happy automating!