Hot answers tagged

29 votes

Ansible: Conditionally define variables in vars file if a certain condition is met

Ansible allows one of following forms to define variable conditionally: test: var1: "{% if my_group_var %}value{% else %}other_value{% endif %}" var2: "{{'value' if (...
user avatar
  • 466
25 votes
Accepted

Loop over Ansible variable array in Jinja2 template

You have a simple syntax error; you should be using brace brackets instead of parentheses. You currently have: (% for mounts in {{ ansible_mounts }} %) Mountpoint: {{ ansible_mounts.mount }} (% ...
user avatar
16 votes

Ansible: Conditionally define variables in vars file if a certain condition is met

I don't think you can, I usually create separate files to hold conditional variable collections and use a when clause to include them on a specific condition: - include_vars: test_environment_vars....
user avatar
  • 70.8k
10 votes
Accepted

SaltStack | How to assign the shell output from cmd.run to Jinja variable?

I think the main problem is that load_avg from cmd is not a number but a string and you have to convert it. Using load_avg|float (or even load_avg|int) like this works: {% set load_avg = salt['cmd....
user avatar
6 votes
Accepted

How to do multiline Jinja2 conditionals in single block?

You can't put the if-then-else in one block unless it is an if-expression. Either: {% if inventory_hostname in groups.aptcache %} {% set cachehost = 'localhost' %} {% else %} {% set ...
user avatar
  • 17.6k
6 votes
Accepted

Reject in ansible list variable

The play below - hosts: localhost vars: apps2: [] apps: - {name: A, extension: .ab} - {name: B, extension: .ab} - {name: C, extension: .c} tasks: - set_fact: ...
user avatar
4 votes
Accepted

How to select network interface given ip address in ansible across Debian and FreeBSD?

There is a difference in data collected by setup on Debian and FreeBSD. In Ubuntu (Debian derivative) the attribute ipv4 is a dictionary. Secondary addresses are stored in the list ipv4_secondaries. ...
user avatar
3 votes
Accepted

use a variable inside json_query in ansible/jinja

You can use a task variable: - name: Check Dev loop assert: that: "'{{ item.value.lvm_device }}' == '{{ ansible_mounts | json_query(query) }}'" with_dict: "{{ fs_configuration }}" ...
user avatar
  • 46
3 votes
Accepted

Ansible: How to get a subelement of a nested list of dictionaries

Figured out how to use json_query properly. - debug: msg: "{{ dns_zone_config | json_query(_query) }}" ...
user avatar
3 votes
Accepted

what does this jinja2 Filter do in Ansible? d({})

Q: "What does this special function called d() do?" A: d() is shortcut for default(). For example the tasks below - set_fact: dictX: '{{ dict1 | d({}) | combine(dict2 |...
user avatar
3 votes

How to trigger a custom error from inside a Jinja template?

Ansible adds the mandatory filter to Jinja, which can be used to do this: {{ ('OK text' if condition_ok) | mandatory('Text of error message') }} gives the failure: fatal: [hostname]: FAILED! => {&...
user avatar
  • 31
3 votes
Accepted

How to trigger a custom error from inside a Jinja template?

Answer from the comment: There is no easy way to accomplish this – only via custom plugin. For details see: https://stackoverflow.com/questions/21778252/how-to-raise-an-exception-in-a-jinja2-macro
user avatar
2 votes
Accepted

Ansible:how to adapt the output format of map (with extract) filters

ansible_ip_addresses is a list, so you have a list (of hosts) with nested lists (with ip addresses). You probably want to flatten it before applying unique and join: {{ (groups['group_one']+groups['...
user avatar
2 votes

converting two lists into a specific dictionary so it can be sorted

ok, i got it: - name: create dict list variable, NICE ONE set_fact: SITES_DICT: "{{ SITES_DICT|default([]) + [{'site': item.1,'rtt': item.0}] }}" with_together: - "{{ rtt }}" ...
user avatar
2 votes

Saltstack state.apply determine whether it is being run in test=True mode ?

In case anyone finds this and wants to know the answer - I checked on the saltstackcommunity slack and some very helpful people responded: terminalmage [2:55 PM] checks looks like it's just called ...
user avatar
2 votes

Ansible: Conditionally define variables in vars file if a certain condition is met

Despite it would be nice, I'm afraid that your way isn't possible (or I'm not aware right way). I'd suggest to first prepare vars file from jinja template and then include it with include_vars. See ...
user avatar
2 votes
Accepted

Inserting Jinja variable into a quoted string

The combination {% opens a Jinja statement. To avoid this interpretation put the brace into a variable, e.g. BR: '{{ "{" }}' and use it in the template shell> cat mschap.j2 ntlm_auth ...
user avatar
1 vote
Accepted

Supplying hosts value to Ansible playbook via Jinja conditionals

What you have provided works fine, as long as dblhr002 is listed in inventory. Host patterns only match existing hosts, they do not add new hosts to the inventory. ec2-user@pandora ~ $ cat test.yml - ...
user avatar
1 vote
Accepted

Ansible jinja2 template from JSON format provided as extra-vars

You are passing in an object/dictionary but your code is expecting a list. You need to either wrap it in a list when you pass it in, or account for the different possible structures when you consume ...
user avatar
1 vote

automated creation of a configuration file with ansible (or something else)

Q: "Read a data file, search a string (example:hostname contains "NR") ... choose the good template in a library in order to create the configuration file." For example, given the ...
user avatar
1 vote
Accepted

Dynamically creating a list in Ansible with jinja

Answering my own question as I have found a solution: enable do jinja extension in ansible.cfg: jinja2_extensions = jinja2.ext.do replace {{ inst_config.append(node_conf) }} with {% do inst_config....
user avatar
1 vote

How to select network interface given ip address in ansible across Debian and FreeBSD?

Inspired from the other answer and the gist linked there vars: freebsd_query: "[*].{device: device, active: active, ipv6: ipv6, ipv4: ipv4[? address == '{{ ip_find_iface }}']}[?ipv4])" # ...
user avatar
  • 206
1 vote

Ansible: Conditionally define variables in vars file if a certain condition is met

Why no one mentions this? _dist: "{{ ansible_lsb.id|lower if ansible_lsb.id == 'Raspbian' else ansible_distribution|lower }}"
user avatar
  • 11
1 vote

How to pass urlencoded string to jenkins api using ansible?

There's an error message: no filter named 'urlencode' If you look here, you'll notice that it's supported in Jinja2 ver 2.7+. Please upgrade your Jinja2 package.
user avatar
1 vote

How to trigger a custom error from inside a Jinja template?

I have written a jinja2 extension-jinja2_error to raise error. Firstly, pip install jinja2_error If you use it with ansible ANSIBLE_JINJA2_EXTENSIONS=jinja2_error.ErrorExtension ansible-playbook ...
user avatar
  • 11

Only top scored, non community-wiki answers of a minimum length are eligible