Skip to content

Install Ansible Roles#

Problem#

  • How do I install Roles from a remote source, such as Ansible Galaxy or a git server such as GitHub or Gitlab?
  • How do I separate external Roles from local Roles?

Solution#

To separate roles that are only used in our project from roles that we install with ansible-galaxy, i.e. from external sources, we first create two directories:

mkdir roles
mkdir roles_galaxy

In ansible.cfg we adjust the paths, separated with :. It is important that the path where the Roles of Galaxy are to be installed is defined first:

ansible.cfg
[defaults]
roles_path = ./roles_galaxy:./roles

We want to be able to version the dependencies of external Roles, so we create a requirements.yml in which we record them:

requirements.yml
roles:
  - name: galaxy.example
    src: https://github.com/exmaple/ansible-role-eyample.git
    version: "1.0.0"
  - name: galaxy.postgresql
    src: https://github.com/geerlingguy/ansible-role-postgresql.git
    version: "2.2.1"
  - name: geerlingguy.docker
    version: "3.0.0"

collections: []

Installation and update of roles:

ansible-galaxy role install --role-file requirement.yml

Explanation#

To separate roles that are only used in our project from roles that we install with ansible-galaxy, i.e. from external sources, we first create two directories. The names of these directories are not relevant and can be customised to your own taste.

To prevent name collisions, we either use a prefix galaxy.<role> for role names in requirements.yml or use the Galaxy naming standard geerlingguy.postgresql. In any case, I recommend to fix the used version to avoid the automatic installation of a future incompatible version.

Warning

Do not rely on Roles being maintained properly. It is often a good idea to fork the role, if necessary to transfer it to a private, internal source code management system and adapt it to your needs and gain more control over changes. However, this is also synonymous with additional maintenance work.