0

Currently I have one big var file (vars/apps.yml) and all my playbooks look like this:

name: my playbook
vars_files:
  - vars/apps.yml
roles:
  - somerole

This works, but now I'd like to change my setup. As my apps.yml got really big and messy, I want to split it into multiple files (one file per app). So I need a possibility to load all vars files into my playbook. It is ok to change all my playbook once, but I do not want to change all my playbooks every time I add a new var file.

My 1. attempt was to have the multiple yml files plus an apps.yml which includes all other files - so I don't have to change anything in my playbooks. Sadly, var files doesn't support include.

My 2. attempt was to replace

vars_files:
  - vars/apps.yml

with

include_vars:
  dir: vars

But that also failed because include_vars is only supported at tasks-level, not at playbook-level.

So now I'm out of ideas how to dynamically load multiple var files at playbook level.


Edit:

As there is some confusion, here is the full playbook I try to execute (using include_vars):

- name: debug
  hosts: localhost
  include_vars:
    dir: vars
  roles:
    - apache

This leads to ERROR! 'include_vars' is not a valid attribute for a Play And as it is mentioned here, include_vars is only available under tasks.

Munchkin
  • 4,528
  • 7
  • 45
  • 93
  • You are confusing playbooks with plays. And what exactly is the problem with `include_vars`? – techraf Aug 20 '18 at 15:07
  • Ah sorry! As I understand, `include_vars` only works when I define it under `tasks` but not at top level of my playbook. – Munchkin Aug 21 '18 at 07:33
  • Thanks for your patience, I edited my question. – Munchkin Aug 21 '18 at 07:39
  • You get an error. Because you need to use `include_vars` under `tasks`, which you seem to know already. What's the point in consciously introducing errors to code and asking about them on StackOverflow? – techraf Aug 21 '18 at 07:41
  • I don't have any `tasks` section (neither in my playbooks nor in my roles) so I'm looking for a way to load multiple vars files (without having to define a list of *all* var files in *all* playbooks) – Munchkin Aug 21 '18 at 07:49
  • Please get some understanding of the tools you are trying to use before asking about them on SO. If you have no `tasks`, then your playbooks do nothing. And in [this comment](https://stackoverflow.com/questions/51933528/dynamic-include-var-files-at-playbook-level?noredirect=1#comment90836966_51933528) surprisingly you seemed to have `tasks`, now not... Also learn the vocabulary, I already told you you are confusing playbooks with plays. – techraf Aug 21 '18 at 07:50
  • Regarding that comment, I'm sorry, I often use `I` as a synonym for `(some)one` (not **I** defined under tasks but **one** has to define it there). Thanks for that hint, I'll learn vocabulary. – Munchkin Aug 21 '18 at 08:00
  • Well, currently my playbooks are working fine without `tasks` (except the include_vars issue)... so your advise is *rewrite all my playbooks to use tasks*? – Munchkin Aug 21 '18 at 08:04

0 Answers0