How can I build a dict iterated with a lookup fileglob?

Viewed 69

So far, I'm unable to figure out how to duplicate my current dict with k:v addition.

Given this dict example:

list0:
  ct0:
    entry:
      - dest: /tmp
        owner: u1
        source: 

I'd like to get this result (source path being fetched with lookup fileglob):

list0:
  ct0:
    entries:
      - dest: /tmp
        owner: u1
        source: /bar/foo2
      - dest: /tmp
        owner: u1
        source: /bar/foo1
      - dest: /tmp
        owner: u1
        source: /bar/foo0

Did not get anything good yet.

1 Answers

Given the variables

    _dest: /tmp
    _owner: u1
    _source: /bar
    _pattern: foo*

Create the entry in each interaction and concatenate the list, e.g.

    - set_fact:
        entries: "{{ entries|d([]) + [_entry] }}"
      loop: "{{ query('fileglob', _source ~ '/' ~ _pattern) }}"  
      vars:
        _entry: "{{ {'dest': _dest,
                     'owner': _owner,
                     'source': item} }}"

(When you've got the list creating the dictionary is trivial).

Related