Rendering Ansible template into the fact variable

I think you might be just looking for the template lookup plugin:

- set_fact:
    rendered_template: "{{ lookup('template', './template.j2') }}"

Usage example:

  • template.j2

      Hello {{ value_for_template }}
    
  • playbook.yml

      ---
      - hosts: localhost
        gather_facts: no
        connection: local
        vars:
          value_for_template: world
        tasks:
          - debug:
              var: rendered_template
            vars:
              rendered_template: "{{ lookup('template', './template.j2') }}"
    
  • The result:

      TASK [debug] *******************************************************************
      ok: [localhost] => {
          "rendered_template": "Hello world\n"
      }
    

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)