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
- 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. 
- 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.ymlfile 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?
- Consistency: Using a - requirements.ymlfile 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.
- Efficiency: Rather than manually installing roles and collections, you can use the - ansible-galaxycommand to install them all at once. This improves efficiency and saves time when setting up new environments.
- 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. 
- Collaboration: In team settings, the - requirements.ymlfile 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
- Create a - requirements.ymlFile: Create a new- requirements.ymlfile at the root of your Ansible project. List all the required roles and collections under the- rolesand- collectionssections.
- Install Dependencies: Run the following command to install the dependencies listed in your - requirements.ymlfile:
ansible-galaxy install -r requirements.ymlThis 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.
 
							 
							 
							