Change variable in Ansible template based on group with children? - ansible

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

Related

Possible to use Jinja2 expressions in role vars file

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 %}"

Ansible facts of all hosts to template

I'm trying to write a dhcpd.conf template for my server to set static leases that is looking like this atm:
`
{% for item in groups.all %}
{% set short_name = item.split('.') %}
host {{ item }} {
hardware ethernet {{ ansible_eth0.macadress }};
fixed-address {{ hostvars[item]['ipaddress'] }};
}
{% endfor %}
`
'ipaddress' is a variable set in the inventory file
After running the playbook everything works fine, but the value for 'ansible_eth0.macaddress' is always the same and not like expected the macaddress matching to the right host.
Does anyone have an idea how i can make the loop working like i want to?
Without the tasks you use this jinja2 template i can just guess. Maybe u are fine with_subelements.

Ansible: Include a file if it exists and do nothing if it doesn't

I have a Jinja2 template in Ansible and I need to include some files conditionally.
Basically, I have a for loop that includes "subparts" that might or might not be present. The following sample demonstrates the basic code that would work if all tools actually had a 'pbr.j2' file, but it crashes because Ansible cannot find some of the files:
{% for tool in tools %}
{% set template = 'tools/' + tool.name + '/pbr.j2' %}
{% include template %}
{% endfor %}
I have also tried the exists and is_file filters, but I always get "false" even when the file exists. Here is a sample code of what I have tried:
{% for tool in tools %}
{% set template = 'tools/' + tool.name + '/pbr.j2' %}
{% if template|exists %}
{% include template %}
{% endif %}
{% endfor %}
The result of this last sample is that no file gets included. If I replace exists with is_file, I get the same behavior.
What do I need to change to accomplish what I want?
I have found a solution to my problem: using ignore missing in the include directive.
In this case, I would use:
{% for tool in tools %}
{% set template = 'tools/' + tool.name + '/pbr.j2' %}
{% include template ignore missing %}
{% endfor %}
Ansible will ignore missing files and only the existing files from that loop will get included.
You are trying to use Ansible filters (exists, is_file) in a Jinja2 template. This works for templates processed by Ansible templating engine, but not for templates processed by the template module.
Jinja2 does not have capability to check the existence of a file, so you need to move your logic to Ansible.
For example: create a find task to search for directories in tools/, and provide an intersection of find-results list with tools list to Jinja2.

Ansible, how to use jinja2 template to access specific value in a list

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/

Jinja2 if/else matching both

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.

Resources