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
2
votes
3 answers

Ansible: How to optimize running speed of a playbook?

Current Setup CPU/RAM 60c/64GB ~2000 servers Time consuming ~1 hour Ansible playbook actions Create folders for each server on localhost and filling them up with templates (vars taken from host inventory's) Encrypting the folders and all this…
Seedtefan
  • 37
  • 4
2
votes
1 answer

Identical variable names when using include_vars

I have multiple files with the same variable names, but with different values in vars/all directory. I used to run with the following command to achieve running all of them. $ for var in `ls vars/all/`; do ansible-playbook foo.yaml -e@vars/all/$var;…
smc
  • 2,175
  • 3
  • 17
  • 30
2
votes
2 answers

How to append a string to each item in a list to display it in a template?

I'm trying to set a template that pulls in a comma separated list of hosts, and appends a port to each host. The list, an ansible fact: {{ nodes }}) looks like this: node1,node2,node3,...,nodeX And I want the final templated file to look like…
user66771
  • 53
  • 5
2
votes
1 answer

ansible Jinja template loop

I'm attempting to loop through a registered variable in a Jinja template via an Ansible playbook. Here is my task: - name: Check if ports are open | inventory ports wait_for: host: "{{ item.name }}" port: "{{ item.port }}" state:…
d1530
  • 151
  • 8
2
votes
1 answer

Ansible throws error after variable content is copied into fact

I have variables, that contain strings with other template engine markers, which unfortunately are three curly braces. If I work with those variables directly, everything is fine. If I copy them into a fact in order to modify them afterwards,…
SipSeb
  • 184
  • 8
2
votes
0 answers

Passing the local context to imported Ansible jinja2 macro

I’m building some Ansible (4.6.0) / Jinja2 (3.0.1) templates that all rely on a shared import for some helper macros. A few of these macros are complex enough that they require many arguments to be passed from the local calling…
Erik Ogan
  • 21
  • 2
2
votes
3 answers

Register output ansible playbook

I have a playbook to run command on a list of servers and put the results in a file. But, sometimes, I can't connect to those servers due to an incorrect login/password or because it is unreachable. When it is unreachable, I have this on the…
Lud
  • 45
  • 7
2
votes
1 answer

Populate a list of dictionary through a loop

I want to generate a list of dictionaries through the loop. I came across that we can use with_sequence to generate the integer sequences in ansible. I am getting the following error: The offending line appears to be: - { "dir": "{{…
Arun Suryan
  • 1,615
  • 1
  • 9
  • 27
2
votes
0 answers

Conditionally comment a block in an Ansible template

Is there a simpler way to achieve this? I don't want to remove the block via {% if SSL %}{% endif %} {% filter comment( decoration = (SSL) | ternary('', '#'), prefix = (SSL) | ternary('', '### the following block has been…
2
votes
2 answers

padding/ljust with ansible template and jinja

I am looking for a way to create a template in ansible with this dictionary. data= { "_dictionary": { "keyone": "abc", "rtv 4": "data2", "longtexthere": "1", "keythree": "data3", "keyfour": "data1234", } } The template…
dorancemc
  • 123
  • 1
  • 5
2
votes
1 answer

How to run an Ansible task if *any* host has a fact

I'm wrapping my head around something that I'm probably overcomplicating. I need to check if any of my hosts has ansible_virtualization_type == "openvz" If this is the case, ALL hosts should execute a specific task. I'm now trying to set a fact…
Aleritty
  • 25
  • 7
2
votes
2 answers

Sorting list in Ansible in natural alphanumeric order

Is there a way to sort a list in Ansible or Jinja in a natural way? For example this is the list test test123 test12 test5 test1234test test22te And I need it to take in account the numbers as whole not as individual so test12 is under test5 and so…
VladoPortos
  • 563
  • 5
  • 21
2
votes
2 answers

Ansible builtin template unable to create valid yaml result with integer values

I want to template .yaml files with ansible. This is a standard job, I assumed, but then I realized that the template module is unable to produce valid integers in the YAML output. Given an inventory var bar is set to 1, using the following…
moestly
  • 1,681
  • 15
  • 19
2
votes
2 answers

search a string in ansible when condition over loop

Team, I want to loop on sda devices only excluding loop devices so I am trying to compare a part of string if it exists in key that is from ansible_devices item variable and stuck at syntax error. I want to print msg only on WHEN condition Is met.…
AhmFM
  • 1,552
  • 3
  • 23
  • 53
2
votes
1 answer

How to use variable substitution in a when condition?

I'm a new ansible user. I need a variable in a task's when condition. I am trying to use home_directory variable, which is defined as home_directory: "{{ ansible_env.HOME }}" in the vars/main.yaml file. I tried to use something like following: when:…