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 -%}"
Related
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 %}"
I am attempting to template a system properties file using Jinja2. I'm using a list, a for loop and an if loop to cycle through various hostnames and to produce them in a list as a variables property. However, when I then deploy this code using ansible, the formatting comes out wrong and the next line is attached to the first line with no line break.
I have tried numerous different ways of layout, but none of them seem to work.
database.url = {%- set list1 = application_host.split(',') -%} {%- for list in list1 -%} {{ list }}:{{ db_port }}{%- if not loop.last -%},{%- endif -%}{%- endfor -%}
database.name = {{application_db_name }}
This is how it actually looks in the file:
database.url =db-cluster-router-1:4526,db-cluster-router-2:4526solr.database.name = DATABASENAME
I am expecting a line break between the two variables. E.g.:
database.url = db-cluster-router-1:4526,db-cluster-router-2:4526
database.name = DATABASENAME
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 found Change variable in Ansible template based on group. However how do I extend the answer in groups with children?
If I extend the group vars file in the link above to
[host0]
host0.my-network.com
[host1]
host1.my-network.com
[es-masters:children]
host0
host1
How can I extend the above post's answer (shown below) to make it work with the group vars file above? Simply putting the FQDN in the jinja2 statement does not work.
{% if ansible_fqdn in groups['es-masters'] %}
node_master=true
{% else %}
node_master=false
{% endif %}
What you should do is:
Provide default in template
# role_name/templates/template.j2
node_master={{ role_name_node_master | default(true) }}
Than override in group_vars
# group_vars/es-masters.yml
role_name_node_master: false
I'm trying to use an if/else statement in an ansible template to populate values based on my hostname. I've written a module that parses the hostname and adds the values as facts. I'm able to use these facts in my playbooks but am having trouble getting an if/else statement to use them correctly. I end up with both the if and else matching so both values end up in my file.
jinja2 template
{% for host in groups['tag_Function_logfe'] %}
{% if hostvars[host]['tt_number'] == "01" %}
is_master = true
{% else %}
is_master = false
{% endif %}
{% endfor %}
Ansible Facts:
ok: [node1] => {"ansible_facts": {"tt_number": "01"}, "changed": false, "msg": "Additional facts added successfully"}
ok: [node2] => {"ansible_facts": {"tt_number": "02"}, "changed": false, "msg": "Additional facts added successfully"}
results:
is_master = true
is_master = false
I don’t have the full picture of what you’re trying to do overall, but Ansible and Jinja seem to behave as documented in this case so there might be a misunderstanding elsewhere.
You have a template that Ansible would render into a configuration file on every host. In this template, a for loop iterates over hosts in given group and declares is_master for each of those hosts. That would result in multiple definitions in every instance of the template, equal to the number of hosts in the “tag_Function_logfe“ group.
That doesn’t seem to make sense to me, since I’d assume there would be only one is_master definition per host.
Perhaps what you wanted to do instead is having this in the template:
{% if inventory_hostname in groups['tag_Function_logfe'] %}
{% if tt_number == "01" %}
is_master = true
{% else %}
is_master = false
{% endif %}
{% endif %}
The inventory_hostname points to the host Ansible would be working with at the moment. We apparently want to check that this host belongs to the “tag_Function_logfe” group, and if it doesn’t we don’t define is_master at all.
Then we want to determine what value is_master should have based on tt_number. Note that we don’t need to use hostvars because we access tt_number’s value only for current host where the template is rendered into the configuration file.
It’s probably possible to further eliminate the group membership test, but that’s harder to tell without looking at the rest of the configuration.