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
5
votes
1 answer

(Ansible) How to get all inventory variables for a group of hosts as a list in template/Jinja2?

I am writing automation with this type of Ansible inventory: [nodes] privateIp= privateIp= I do this because sometimes hosts have a different public IP vs. private IP (e.g., AWS). Now while…
user87219
  • 181
  • 2
  • 10
5
votes
1 answer

Ansible concat default variable and literal string

I'm not able to do a very simple thing. In my task I'm trying to concat two default variables with a literal string to create a path - name: "Uploading File" copy: src: "{{ installer_iso_src_location }}"/"{{ installer_version…
Anand Rao
  • 303
  • 3
  • 4
  • 8
4
votes
1 answer

Map function over values of dict in ansible jinja template

I want to convert the values of a dictionary into strings and lowercase them, then map over the dict to join each item pair with an =, and then join the items with a ,. Example input: {"A": "Aardvark", "B": "Beaver", "C": "Cat"} Desired…
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
4
votes
2 answers

Template error while templating string: unexpected char u - Ansible

When executing a playbook to run a command in a remote host and pass the output using shell, getting below error. fatal: [master1]: FAILED! => {} MSG: template error while templating string: unexpected char u'a' at 4. String: …
Haris
  • 85
  • 2
  • 9
4
votes
1 answer

Ansible template to ignore end of lines difference LF CRLF. A plugin?

How is it possible to make template task not produce changed: result if the before and after only differ in line endings? I really don't care the line endings in config files on the target hosts, the applications ignores them, but those files can…
Tag Wint
  • 407
  • 3
  • 9
4
votes
3 answers

Escape or differentiate between jinja template variables in Ansible and .

History: We've been using Ansible to deploy our services and config files over the last few months and we've been making use of the Ansible variables. The variables get placed in our (config_name).yml.j2 files which allows us to easily make changes…
Oli Ur M80
  • 75
  • 2
  • 9
4
votes
2 answers

How to exclude localhost from groups when hosts action is localhost with ansible

I have to list all server in a file by using a template.j2. The purpose is to generate a config file up to date with the ansible inventory file. All files are on the ansible server. I have a generate-projectconf.yml, a template.j2 and the inventory…
4
votes
2 answers

Ansible register variable in task and use it in template

In the Ansible task, how to register a variable so I can use it as check statement in Template. The tasks are: - name: Check if certificate file exists stat: path=/etc/nginx/ssl/{{ sitename }}.pem register: ssl_cert_check - name: Create vhost…
user1376704
3
votes
1 answer

How can I condition the command to run based on facts and save its output to the same variable?

The aim of the playbook below is to extract facts from a number of devices and use it to run specific shell commands. I believe I have most of it figured out with two exceptions. How can I run one set of commands versus the other based on a when…
Tash
  • 31
  • 5
3
votes
2 answers

Create Local File With Ansible Template From Variables

I'm running an ansible playbook against a number of ec2 instances to check if a directory exists. --- - hosts: all become: true tasks: - name: Check if foo is installed stat: path: /etc/foo register: path - debug:…
Metro
  • 873
  • 8
  • 19
3
votes
2 answers

Find substring in ansible_hostname

I need to check all the cities in the country variable list to see if they contain the city name in the Ansible hostname variable. It means running hosts can contain a city name in its own hostname. - name: Find city gather_facts: true hosts:…
Disbalance
  • 63
  • 1
  • 7
3
votes
1 answer

referencing variables from defaults/main.yml in ansible role

I have a role setup as follows roles/test/task/main.yml - name: Generate people files template: src=test.j2 dest=/tmp/{{ item.name }}.cfg loop: "{{people}}" roles/test/template/test.j2 First Var: {{ item.var1 }} Second Var: {{ item.var2…
pizzaguy39
  • 87
  • 5
3
votes
1 answer

Ansible string split

I have a split function in ansible based on a delimiter. But only want to get the first occurance of the delimiter string and the rest as the second string. string: "hello=abcd=def=asd" string1= string.split("=")[0] string2= string.split("=)[1..n]…
Vini
  • 1,978
  • 8
  • 40
  • 82
3
votes
1 answer

Ansible task to cleanup file backups

Is there a way to automatically cleanup backup files ansible creates when useing e.g. the copy or template modules, or to keep the last three backup files for example? Ansible file backups from template task
Andre
  • 39
  • 4
3
votes
1 answer

Ansible - problem with ansible_facts for packages in jinja template

I try to get all installed packages with Ansible and write them in a "pretty" way to a file. Calling the module works: - name: Gather the rpm package facts package_facts: manager: auto In a Jinja template I am using a loop, what works too: {%…