What I can use to replace {{ }} while I still want to use variables?
when: ansible_lvm.lvs.{{ resize_lvname }}.size_g < 10 and
ansible_devices.{{ new_dev }}.size == "70.00 GB
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: ansible_lvm.lvs.{{
resize_lvname }}.size_g < 10 and ansible_devices.{{ new_dev }}.size ==
"70.00 GB" and not "[new_dev].value.partitions"
In a when conditional you are already inside an implicit Jinja template context...which means if you want to refer to a variable, you just need the variable name. For example:
when: ansible_lvm.lvs[resize_lvname].size_g < 10 and
ansible_devices[new_dev].size == "70.00 GB
Recall that some.var.key is equivalent to some.var["key"]; we need to use the [...] syntax here because we want to use the value of resize_lvname as key on the ansible_lvm.lvs dictionary. If we were to write ansible_lvm.lvs.resize_lvname, we would be attempting to look up a key with the literal name resize_lvmname (in other words, that would be equivalent to ansible_lvm.lvs["resize_lvname"]).
Related
I would like to use the 2 values of a group variable in two different places. My current group vars looks like this.
[test1:vars]
fooname=foo1
barname=bar1
I am using jinja template in the playbook as {{ fooname }} and {{ barname }} based on my requirements in the playbook in multiple places. Now instead of two different variables, i would like to use it as one variable as names and i would like to use the values of it in different places.
Expected group variables :
[test1:vars]
names=foo1,bar1
Is there a way that now i can call {{ names }} variable with some function or condition like {{ names is search(foo) }} or {{ names is search(bar) }} inside the playbook like we use in condition so that i can avoid declaring 2 variables instead of one. I will use these variables in different places in my playbook.
I tried using the above one which prints "True" instead i need the value of my variable which has only foo or bar when i search accordingly.
Note : I have close to 400 groups which same pattern with makes the extra variable makes my inventory lengthy. So, i would like to minimize it.
Ansible 2 supports Arrays in the inventory File.
[test1:vars]
names=["foo","bar"]
But there are some limitations.
[example:vars]
# working
var1=["foo","bar"]
var2=[1,2]
var3=[True, False]
# not working
var4=[yes, no] # Boolean need to be True and False
var5=[foo, bar] # Interpreted as one string
var6='["foo","bar"]' # Interpreted as one string as well
Usage example:
- debug:
- msg: "Item: {{ example[0] }}"
- debug:
- msg: "Item: {{ example | first }}"
I'm wondering if it's possible to use a Jinja2 {% if %} expression inside a vars file?
So say I have:
az:
az1: foo
az2: bar
az3: foobar
{% if az == az['az1'] %}
floating_ip_pool = bar
{% endif %}
Basically, I'm trying to avoid having to set these variables each time since they'll always be based on the az.
Thanks.
That's not valid syntax. A vars file must first be parsed as a YAML document, and introducing that Jinja syntax results in something that is no longer valid YAML.
You can do something like this instead:
az:
az1: foo
az2: bar
az3: foobar
floating_ip_pool: "{% if target_az == az['az1'] %}bar{% endif %}"
How would a node attribute being set by a case in Chef be translated to a variable in Ansible?
Example:
case node['custom_attribute']
when 'security_standard_1'
security_standard = 'ss1'
when 'security_standard_2'
security_standard = 'ss2'
else
security_standard = 'off'
end
I've only seen things that have two possible values being set with jinja.
security_standard: "{{ security_standard_1 | security_standard_2 }}"
Is this thought process even doable in Ansible?
You can define variables using jinja template with if-else conditions to suit your needs.
Here's an example:
security_standard: "{%- if (security_standard_1 is defined) -%}ss1
{%- elif (security_standard_2 is defined) -%}ss2
{%- else -%}off
{%- endif -%}"
New to Jinja2 templating
I can iterate over a list using a for conditional which is simple enough but i am trying to do the below...
I have a variable that contains a un-ordered lists of values which are group names. I would like to access the group_names lists/variable and check if a specific item in this list exists and then perform an action if that value is found.
group_names: [ "groupname1", "groupname2", "groupname3", "groupname4"]
Sounds like you want:
{% if "somevalue" in group_names %}
whatever stuff
{% endif %}
http://jinja.pocoo.org/docs/2.9/templates/
I would like to use a system fact for a host times a number/percentage as a base for a variable. What I am trying to do specifically is use the ansible_memtotal_mb value and multiply it by .80 to get a ramsize to then use in setting a Couchbase value. I have been trying different variations of the line below. I'm not ever sure that it is possible, but any help would be appreciated.
vars:
ramsize: '"{{ ansible_memtotal_mb }}" * .80'
You're really close! I use calculations to set some default java memory sizes, which is similar to what you are doing. Here's an example:
{{ (ansible_memtotal_mb*0.8-700)|int|abs }}
That shows a couple of things- first, it's using jinja math, so do the calculations inside the {{ jinja }}. Second, int and abs do what you'd expect- ensure the result is an unsigned integer.
In your case, the correct code would be:
vars:
ramsize: "{{ ansible_memtotal_mb * 0.8 }}"
One little thing to add.
If you presume the math multiplication has precedence before jinja filter (| sign), you're wrong ;-)
With values like
total_rate: 150
host_ratio: 14 # percentual
"{{ total_rate*host_ratio*0.01|int }}" => 0 because 0.01|int = 0
"{{ (total_rate*host_ratio*0.01)|int) }}" => 21 as one expects
use: {{ ansible_memtotal_mb|int * 0.8 - 700 }}