Search code examples
ansiblejinja2ansible-2.xansible-factsansible-template

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


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.


Solution

  • 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).