Can slurp be used as a direct replacement for lookup?

Viewed 34

I have the following in a template file foo.cfg.j2:

nameserver dns1 {{ lookup('file','/etc/resolv.conf') | regex_search('\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b') }}:53

The problem is it gets the value from the local /etc/resolv.conf. I wanted to get the value from the target, I learned that I have to use slurp, but the second "problem" is that I need to register an variable in the task, and then pass it to the template file, like this:

tasks:
  - slurp:
      src: /etc/resolv.conf
    register: slurpfile

and the template file is now like this:

nameserver dns1 {{ slurpfile['content'] | b64decode | regex_search('\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b') }}:53

While this works, I feel a bit uneasy as the solution is split into two places: the task does something, and then the template file does the second part. Is there a remote version of lookup that is a direct replacement?

1 Answers

Instead of slurp I'd rather fetch the remote files

  - fetch:
      src: /etc/resolv.conf
      dest: "{{ fetched_files }}"

,declare a variable, e.g.

my_etc_recolv_conf: "{{ fetched_files }}/{{ inventory_hostname }}/etc/resolv.conf"

,and use it in the template

nameserver dns1 {{ lookup('file', my_etc_recolv_conf) | ...
Related