Questions tagged [ansible-template]

Use this tag for questions regarding writing and processing Ansible templates, Jinja2 syntax within Ansible, custom filters.

Ansible templates are processed by the Jinja2 templating language. Beside the default Jinja2 filters Ansible extends functionality with a lot of custom filters.

Templates are rendered from Ansible tasks through the template module or through the template lookup plugin.

638 questions
161
votes
12 answers

Ansible: get current target host's IP address

How do you get the current host's IP address in a role? I know you can get the list of groups the host is a member of and the hostname of the host but I am unable to find a solution to getting the IP address. You can get the hostname by using…
SJC
  • 2,767
  • 4
  • 20
  • 31
115
votes
8 answers

How to use template module with different set of variables?

My use case is the following : I have a template file, and I would like to create 2 different files from that template, with the variables being filled by a different set of variables for each file. For example, lets say I want to template the file…
Kestemont Max
  • 1,302
  • 2
  • 8
  • 10
80
votes
5 answers

How can I test jinja2 templates in ansible?

Sometimes I need to test some jinja2 templates that I use in my ansible roles. What is the simplest way for doing this? For example, I have a template (test.j2): {% if users is defined and users %} {% for user in users %}{{ user }} {% endfor %} {%…
Alex
  • 5,728
  • 5
  • 20
  • 20
59
votes
3 answers

How to get the first element of a list from the output of setup module in Ansible?

I received the following data from the setup module: "ansible_nodename": "3d734bc2a391", "ansible_os_family": "RedHat", "ansible_pkg_mgr": "yum", "ansible_processor": [ "AuthenticAMD", "AMD PRO A10-8700B R6, 10 Compute Cores…
thinkingmonster
  • 5,063
  • 8
  • 35
  • 57
52
votes
9 answers

ansible - delete unmanaged files from directory?

I want to recursively copy over a directory and render all .j2 files in there as templates. For this I am currently using the following lines: - template: > src=/src/conf.d/{{ item }} dest=/dest/conf.d/{{…
Michael Krupp
  • 2,042
  • 3
  • 19
  • 36
42
votes
6 answers

Passing variables to ansible roles

I have my directory structure as this . ├── README.md ├── play.yml └── roles ├── bootstrap_server │   └── tasks │   └── main.yml ├── create_new_user │   └── tasks │   └── main.yml ├── update │   └── tasks …
Tasdik Rahman
  • 2,160
  • 1
  • 25
  • 37
38
votes
6 answers

how to append to a list in jinja2 for ansible

Below is the jinja2 template that i wrote to use in ansible. {% set port = 1234 %} {% set server_ip = [] %} {% for ip in host_ip %} {% do server_ip.append({{ ip }}:{{ port }}) %} {% endfor %} {% server_ip|join(', ') %} Below is the my desired…
learning fun
  • 519
  • 1
  • 5
  • 12
34
votes
1 answer

Rendering Ansible template into the fact variable

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.
kay
  • 693
  • 2
  • 7
  • 13
24
votes
1 answer

Extra spaces appearing in Ansible templates

I am generating config files and I want them to be indented just so. I started with a Jinja2 template that rendered correctly when called from a simple python program. When I call it from ansible, I will get 2 extra spaces on all but the first line…
JerryS
  • 241
  • 1
  • 2
  • 3
19
votes
2 answers

Ansible, set_fact using if then else statement

I am trying to set a variable in Ansible with set_fact at runtime based upon another variable. If uses first value no matter what the actual value is. Here is my code example: - name: Global_vars - get date info set_fact: jm_env:…
thepod71
  • 191
  • 1
  • 1
  • 4
19
votes
1 answer

Ansible template adds 'u' to array in template

I have the following vars inside of my ansible playbook I got the following structure domains: - { main: 'local1.com', sans: ['test.local1.com', 'test2.local.com'] } - { main: 'local3.com' } - { main: 'local4.com' } And have the following…
Steve
  • 1,213
  • 5
  • 16
  • 29
19
votes
5 answers

Loop dictionary in ansible template

I'm trying to loop a dictionary through an ansible template using jinja2 to create a number of datasources but receive this error [{'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute 'value'", 'failed':…
tweeks200
  • 1,837
  • 5
  • 21
  • 33
18
votes
3 answers

How can I take a list of server names and append a resource URI and port to each?

I merged two lists from an Ansible inventory: set_fact: fact1: "{{ groups['group1'] + groups[group2']|list }} The output is: fact1: - server01 - server02 - server03 With the above results, I need to append https:// to the front, and a port…
Adam S
  • 183
  • 1
  • 1
  • 5
18
votes
5 answers

Can the templates module handle multiple templates / directories?

I believe the Ansible copy module can take a whole bunch of "files" and copy them in one hit. This I believe can be achieved by copying a directory recursively. Can the Ansible template module take a whole bunch of "templates" and deploy them in one…
danday74
  • 52,471
  • 49
  • 232
  • 283
18
votes
1 answer

How to remove or exclude an item in an Ansible template list?

I'm writing an Ansible template that needs to produce a list of ip's in a host group, excluding the current hosts IP. I've searched around online and through the documentation but I could not find any filters that allow you to remove an item in a…
ScoSol
  • 195
  • 1
  • 1
  • 6
1
2 3
42 43