Explicitely use role defaults

Hello,

I’m working on a playbook where I’d like to fetch some files from my hosts to the control node.

I want the destination (on the control node) to be controlled by a variable local_release_dir in the localhost context (aka, set in the inventory for localhost, or using role defaults).
However, since the tasks run in the context of the host, if I just use {{ local_release_dir }}, I’ll have (roughly) {{ hostvars[current_host]['local_release_dir'] | default(local_release_dir) # aka the role defaults }}, which does not work, because hostvars[current_host]['local_release_dir'] != hostvars['localhost']['local_release_dir'] is a situation I want to handle correctly.

The workaround I have at the moment is to add the default explicity, but it’s not great, because there is now 2 sources of truth who can diverge.

  - name: Download_file | Fetch file to localhost
    fetch:
      flat: true
      src: "{{ local_release_dir }}/{{ item.value.dest }}"
      # TODO: delete d() filter
      dest: "{{ (hostvars['localhost']['local_release_dir'] | d('/tmp/releases/')) + '/' }}" # the default ilter here should be replaced with a way to explicitly reference the role defaults. 
    loop: "{{ some jinja expression listing the needed files, per host }} 

Possibles workarounds:

  • use include_vars with name to make the defaults variables available under a named variable ; I’m not a fan since I need to add a task.

Full context for anyone interested : Refactor download (file) by VannTen · Pull Request #12299 · kubernetes-sigs/kubespray · GitHub

Would appreciate any ideas, including doing the same thing (getting files on localhost) in a different way / with a different modules.