3

Is there a way to provide variables at collection level. So that all roles in this collection can access the variable?

Example Usecase: A collection with multiple roles for a company. Many roles need a default network storage location.

Possible workarounds:

  1. Having a role that all other roles in the collection depend upon that calls set_fact
  2. Setting facts on the instances
ProfHase85
  • 11,763
  • 7
  • 48
  • 66
  • 1
    Why `set_facts` ? A `default_vars_only` role added as a dependency with a single `vars/main.yml` file should do the trick. If you need those vars to be a bit more overridable, you can use `defaults/main.yml`. – Zeitounator Apr 22 '21 at 13:25
  • You are right - that is even better. The nicest (and most explicit) way would be to have some collection - level variables. – ProfHase85 Apr 22 '21 at 13:33
  • Note that you can have a `vars` folder at top level of the `playbook` folder. That might suit your needs as well depending on your exact requirement: https://docs.ansible.com/ansible/devel/dev_guide/developing_collections.html#collection-structure – Zeitounator Apr 22 '21 at 13:35
  • 1
    Yes, I have seen that, thanks. The problem is that there is no docs on the playbooks directory. It is not clear 1. if the vars are imported if one runs a playbook that is not in this collection 2. if it is intended like that (and it is not even clear how to run a playbook from the collection) – ProfHase85 Apr 22 '21 at 13:41
  • 2
    1. files in the vars dir adjacent to a playbook are not imported automagically anyway. But the dir is added to the search path wihch eases writting like `vars_files: [a.yml, b.yml]` at play level or `[import_vars: {path: somefile.yml}]` at task level. Vars dir inside a playbook dir in a collection are no exception. See https://docs.ansible.com/ansible/latest/user_guide/playbook_pathing.html#the-magic-of-local-paths 2. playbook in a collection can be run/included from any other location. I'm not sure they have anything particular. For anything automagic, go with the role dependency IMHO – Zeitounator Apr 22 '21 at 14:00

1 Answers1

0

Put your variables in playbooks/group_vars/all.yml