3

I'm using Vagrant+Ansible on a project. I want to make it optional for the user to provide a role called "workspace" which will be included in the provisioning. This allows the user to set his own preferred prompt, editor etc.

If a role that doesn't exist is specified in a playbook, the whole thing crashes with

ERROR! The role 'workspace' was not found ...

I would like it to simply ignore it and move on. I tried adding failed_when: false, but unsurprisingly it had no effect. It is possible to formulate a when: ... condition that can check if the role exists? Or is there another way to solve this nicely?

Hubro
  • 56,214
  • 69
  • 228
  • 381

1 Answers1

1

Looking at ansible documentation for Conditionals, I don't think it's directly possible to check whether a role exists.

Workarounding this via check if a directory for the role exists would be a little tricky, as there are multiple places where ansible searches for roles and some are based on current platform, environment or configuration.

That said, I tried a bit different approach which seems to work: Instead of insisting on a predefined role name, one can ask for a role name via ansible variable. Then you can include this role like this:

- name: Minimal playbook example
  hosts: localhost
  connection: local
  vars:
    role_name: ""
  roles:
    - role: "{{ role_name }}"
      when: role_name != ""

When user doesn't provide it's own role name, this role is not included. Also note that for this to work, you need to specify a default value for a role name variable, otherwise the ansible would fail on undefined variable (adding is defined check into when clause doesn't help).

marbu
  • 1,939
  • 2
  • 16
  • 30