1

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?

puravidaso
  • 1,013
  • 1
  • 5
  • 22
  • Have you considered using `set_fact:` in order to decouple the way the `dns1_ip` is acquired from the way the template is going to use it? – mdaniel Sep 14 '22 at 02:43
  • 3
    There is no such direct replacement. You can't expect any lookup plugin to provide you with the functionality of a module. [There are substantial differences](https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html#modules-and-plugins-what-is-the-difference). – Vladimir Botka Sep 14 '22 at 04:25
  • @mdaniel yes, I have considered using `set_fact:` but I found that it also set a variable in task to be used in template. I do not see the conceptual difference. – puravidaso Sep 14 '22 at 05:29

1 Answers1

1

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) | ...
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63