Ansible var files with replace module

I have the a group_vars folder which contains:

  • all.yml
  • servergroup.yml

I have a host_vars folder which contains:

  • serverA.yml

These have a large amount of variables in each. The current project has the requirement of being able to have global variables which apply to all servers, group specific variables which apply to the groups and individual variables which apply to individual servers, if a host var is different to the global then it will be replaced, but normal ansible variable precidence will sort that out I understand - So Nothing unique at this point.

The way the project works is: I have multiple files within a folder which have tokens within them that need to be replaced by the variables within each of the vars files. I have earmarked the replace module to parse the folders/files and replace the tokens, for this I am using the below and have it in a role called “token-place”, the tasks/main.yml contains

    - name: Replace config
  replace:
    path: "{{item[0]}}"
    regexp: "{{item[1]}}"
    replace: "{{item[2]}}"
  with_nested:
    - "{{file_paths}}"
    - "{{tokens | dictsort }}"

The defaults/main.yml looks like this

file_paths: [] # list of file paths e.g. ['file1, /tmp/file2' ]
tokens: {} # dictionary of tokens e.g. {a: 1, b: 2, c: 3}

The file paths would need to be dynamic however, this is something I can do - But any advice to save time would be welcomed.

My problem is I dont know how to load all the variables from all 3 vars files and load them into the

- "{{tokens | dictsort }}"

So that when it runs, it unpacks all the dicts and replaces all the instances of the specific tokens within all the files I place within

- "{{file_paths}}"

Any advice would be appreciated. If I use include_vars: I dont know how to unpack the main variable and my main problem is I dont know the correct question to ask in order to find the answer. Thanks