1

What I'm looking for is a task (or tasks) to read whatever .yml files exist in a specified folder and combine them into a file containing all the variables. For example:

pets/steves_pets.yml:

cats:
  - spot
  - snowy
hamsters:
  - mephistopheles

And

pets/jacks-pets.yml:

dogs:
  - rex
  - woof
hamsters:
  - prometheus

Becomes

wherever/all-pets.yml:

cats:
  - spot
  - snowy
dogs:
  - rex
  - woof
hamsters:
  - mephistopheles
  - prometheus

In fact it's not even necessary to generate an all-pets.yml - as long as the full combined list is available to Ansible for use in roles / tasks. But a combined list file approach would be fine if easier.

Any pointers would be greatly appreciated!

Sam
  • 111
  • 2

2 Answers2

0

I think you can just include_vars at directory level with dir

tasks:
  - name: "Read my pets from pets folder"
    include_vars:
      dir: "pets"
      name: all_pets
Iván
  • 101
0

Sounds like you want the combine filter.

You could read each file into a specific variable, then use the combine filter to create all-pets. At that point, it would be in memory of course, so you might want to use the answer to this question to write it to a file:

- name: Read vars
  include_vars:
    file: "pets/{{ item }}_pets.yml"
    var: "{{ item }}"
  loop:
    - steves
    - jacks

- name: Combine vars
  set_fact:
    all_pets: {{ steves | combine(jacks) }}


- name: Write the file
  copy:
    content: "{{ all_pets | to_yaml }}"
    dest: pets/all_pets.yml
Bruce Becker
  • 3,783
  • 4
  • 20
  • 41