Installation of dependencies of an ansible role

Posted February 4, 2021 by Adrian Wyssmann ‐ 3 min read

Some observations regarding using dependencies in ansible roles

My ansible role papanito.ttrss is the first one using external dependencies i.e. roles made by others, as you can see in the meta/main.yml

....
dependencies:
  - role: geerlingguy.php
    vars:
      php_use_managed_ini: "{{ ttrss_php_use_managed_ini }}"
      ....
  - role: geerlingguy.postgresql
    vars:
      postgresql_restarted_state: "{{ ttrss_postgresql_restarted_state }}"
      ....

I was under the assumption that with this declaration in the meta/main.yml, dependencies would be installed automatically when using the role - apparently not. I have the following playbook for testing:

---
- hosts: localhost
  remote_user: root

  roles:
  - { role: ../ansible-role-ttrss }

However this fails, cause the respective dependencies are missing:

ansible-playbook tests/test.yml -i tests/inventory --syntax-chec
ERROR! the role 'geerlingguy.postgresql' was not found in ansible.legacy:/home/papanito/Workspaces/mydomain.com/ansible-role-ttrss/tests/roles:/home/papanito/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/papanito/Workspaces/mydomain.com:/home/papanito/Workspaces/mydomain.com/ansible-role-ttrss/tests

The error appears to be in '/home/papanito/Workspaces/mydomain.com/ansible-role-ttrss/meta/main.yml': line 105, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      # php_source_configure_command: "{{ php_source_configure_command }}"
  - role: geerlingguy.postgresql
    ^ here

Looking at the documentation of ansible it says

Roles can also be dependent on other roles, and when you install a role that has dependencies, those dependencies will automatically be installed to the roles_path.

There are two ways to define the dependencies of a role:

  • using meta/requirements.yml
  • using meta/main.yml

Still this does not make it magically appear, the above only take place if you run ansible-galaxy install -r requirements.yml. So for your ansible project you define papanito.ttrss in the requirements.yml

- src: https://github.com/papanito/ansible-role-ttrss.git
  name: papanito.ttrss
  scm: git
  version: master

… and then run ansible-galaxy install -r requirements.yml. Now we can see that the role papanito.ttrss and it’s dependencies are installed successfully.

ansible-galaxy install -r requirements.yml
Starting galaxy role install process
- extracting papanito.ttrss to /home/papanito/Workspaces/mydomain.com/infra/roles/papanito.ttrss
- papanito.ttrss (master) was installed successfully
- adding dependency: geerlingguy.php
- adding dependency: geerlingguy.postgresql
- downloading role 'php', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-php/archive/4.5.1.tar.gz
- extracting geerlingguy.php to /home/papanito/Workspaces/mydomain.com/infra/roles/geerlingguy.php
- geerlingguy.php (4.5.1) was installed successfully
- downloading role 'postgresql', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-postgresql/archive/3.0.0.tar.gz
- extracting geerlingguy.postgresql to /home/papanito/Workspaces/mydomain.com/infra/roles/geerlingguy.postgresql
- geerlingguy.postgresql (3.0.0) was installed successfully

So the issue with testing your own role - with dependencies - is that you also need a requirements.yml and run ansible-galaxy install -r requirements.yml to get the dependencies installed. Here my requirements.yml - so in addition to the declaration in meta/main.yml we also have to define the roles there:

roles:
- name: geerlingguy.postgresql
collections:
- name: geerlingguy.php_roles

Now ansible-playbook work as expected

$ ansible-galaxy install -r requirements.yml
Starting galaxy role install process
- downloading role 'postgresql', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-postgresql/archive/3.0.0.tar.gz
- extracting geerlingguy.postgresql to /home/papanito/.ansible/roles/geerlingguy.postgresql
- geerlingguy.postgresql (3.0.0) was installed successfully
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'geerlingguy.php_roles:1.0.0' to '/home/papanito/.ansible/collections/ansible_collections/geerlingguy/php_roles'
Downloading https://galaxy.ansible.com/download/geerlingguy-php_roles-1.0.0.tar.gz to /home/papanito/.ansible/tmp/ansible-local-1196058mmlqcy1v/tmp8fyay96j
geerlingguy.php_roles (1.0.0) was installed successfully

$ ansible-playbook tests/test.yml -i tests/inventory --syntax-check
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "

playbook: tests/test.yml