Is there a way to render Ansible template into the fact? I tried to find a solution but it looks like temp file is the the only way.
Rendering Ansible template into the fact variable
Asked Answered
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" }
This solution is not suitable for every use case, as set_facts doesn't change the variable value. So if another task depends on on a change, it will not run. –
Tripura
This also assumes the template to be rendered is a template file. What if the template I want to render is just a string, like
"{{ lookup('some_lookup', some_var) }}"
? –
Discretional © 2022 - 2024 — McMap. All rights reserved.