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: … Read more

Passing variables to ansible roles

I got it working by using this playbook: — – hosts: testdroplets roles: – update – bootstrap_server – role: create_new_user username: username – role: vimserver username: username Docs: http://docs.ansible.com/ansible/playbooks_roles.html#roles Here is another option, with a directory structure like . ├── README.md ├── ansible.cfg ├── play.yml └── roles ├── bootstrap_server │   └── tasks │   └── main.yml … Read more

How can I test jinja2 templates in ansible?

At this time exists 4 different variants: 1_Online (using https://cryptic-cliffs-32040.herokuapp.com/)Based on jinja2-live-parser code. 2_Interactive (using python and library jinja2, PyYaml) import yaml from jinja2 import Template >>> template = Template(“”” … {% if users is defined and users %} … {% for user in users %}{{ user }} … {% endfor %} … {% endif … Read more

Ansible: get current target host’s IP address

A list of all addresses is stored in a fact ansible_all_ipv4_addresses, a default address in ansible_default_ipv4.address. — – hosts: localhost connection: local tasks: – debug: var=ansible_all_ipv4_addresses – debug: var=ansible_default_ipv4.address Then there are addresses assigned to each network interface… In such cases you can display all the facts and find the one that has the value … Read more

tech