Understanding Requirements.yml in Ansible

In Ansible, the requirements.yml file is a configuration file that defines the dependencies your project needs, such as roles and collections. This file is primarily used for the automation of role and collection installations. It specifies where Ansible should fetch the required roles or collections from, whether from Ansible Galaxy, a Git repository, or a local directory.

By maintaining a requirements.yml file, you can ensure that your Ansible playbooks and roles are consistently configured across different environments. This is particularly useful in team-based environments, ensuring that all members have the same set of dependencies.

How Does requirements.yml Work?

The requirements.yml file is processed using the ansible-galaxy command. When executed, this command reads the file and installs the listed roles or collections automatically. This ensures that your environment is properly configured without requiring manual installation of each dependency.

Key Sections in requirements.yml

  1. Roles Section: This section lists all the roles that your playbook requires. You can specify the name, version, and source for each role. For example, you can pull roles from Ansible Galaxy, or from a Git repository if the role is custom or not publicly available.

  2. Collections Section: In newer versions of Ansible (2.9+), the concept of collections was introduced. A collection is a package that can contain roles, modules, and plugins. The requirements.yml file allows you to list all collections required by your playbook, ensuring you have all necessary dependencies.

Example of requirements.yml File for Roles

---
roles:
  - name: geerlingguy.apache
    version: "2.0.0"
  - name: geerlingguy.mysql
    src: "git+https://github.com/geerlingguy/ansible-role-mysql.git"
    version: "v3.0.0"

Example of requirements.yml File for Collections

---
collections:
  - name: ansible.builtin
    version: "1.0.0"
  - name: community.general
    version: "3.5.0"

Why is requirements.yml Important?

  1. Consistency: Using a requirements.yml file ensures that every member of the team or anyone running the playbook gets the same set of roles or collections. It eliminates the possibility of mismatched versions or missing dependencies.

  2. Efficiency: Rather than manually installing roles and collections, you can use the ansible-galaxy command to install them all at once. This improves efficiency and saves time when setting up new environments.

  3. Version Control: You can specify the exact versions of roles or collections required, ensuring that your playbook is compatible with those versions. This prevents breaking changes caused by version mismatches.

  4. Collaboration: In team settings, the requirements.yml file helps in collaborating on playbooks. Each developer or operations engineer can use the same set of roles and collections without worrying about installation issues.

How to Use requirements.yml

  1. Create a requirements.yml File: Create a new requirements.yml file at the root of your Ansible project. List all the required roles and collections under the roles and collections sections.

  2. Install Dependencies: Run the following command to install the dependencies listed in your requirements.yml file:

ansible-galaxy install -r requirements.yml

This command will install all the roles and collections listed in the requirements.yml file into your environment.

3. Use the Installed Roles/Collections: Once installed, you can use these roles or collections in your Ansible playbooks by simply referencing their names.

Ashutosh Dixit

I am currently working as a Senior Technical Support Engineer with VMware Premier Services for Telco. Before this, I worked as a Technical Lead with Microsoft Enterprise Platform Support for Production and Premier Support. I am an expert in High-Availability, Deployments, and VMware Core technology along with Tanzu and Horizon.

Leave a Reply