refer variable in host file from jinja2 - ansible

I am using jinja2 template for haproxy configuration and I have variable declared in inventory file. how do I refer this variable in template. i have paste below script but it is not working.
My Inventory:-
[as]
10.0.0.1 asRole=batch
10.0.0.2 asRole=batch
10.0.0.3 asRole=transaction
10.0.0.4 asRole=transaction
[hap]
10.0.0.6
[hap] is the ip for the haproxy and my ansible is to set the haproxy configuration on [hap] node based on the [as] node.
My Jinja2 tempalte:-
{% for host in groups['as'] %}
{% if "transaction" in "{{ asRole }} %}
server {{ hostvars[host]['inventory_hostname'] }} {{ host }}:{{ httpd_port }}check
{% endif %}
{% endfor %}
Br,
Tanmoy

Be careful with the asRole Var:
{% for host in groups['as'] %}
{% if "transaction" in asRole %}
server {{ hostvars[host]['inventory_hostname'] }} {{ host }}:{{ httpd_port }}check
{% endif %}
{% endfor %}

Related

How to exclude an host in Ansible Jinja2 template for-loop?

I have a template named foo.yml.j2 used in an Ansible task to generate a foo.yml file:
{% for host in ansible_play_hosts_all %}
{{ host }},
{% endfor %}
Everything works fine except I need something like the following statement: For every host in ansible_play_hosts_all except for host==bar do this or that.
Is this achievable or the only way to do this is to categorize my hosts in different groups and use ansible_play_hosts_group?
You can use the reject filter to take your host out of the list so that it is not part of your loop:
{% for host in ansible_play_hosts_all | reject('==', 'bar') %}
{{ host }},
{% endfor %}
There are more options. The trivial one is using the condition in Jinja
{% for host in ansible_play_hosts_all %}
{% if host != 'bar' %}
{{ host }},
{% endif %}
{% endfor %}
Fit the format to your needs.
Example of a complete playbook for testing
- hosts: host_1,host_2,host_3
gather_facts: false
tasks:
- debug:
msg: |
{% for host in ansible_play_hosts_all %}
{% if host != 'host_2' %}
{{ host }},
{% endif %}
{% endfor %}
run_once: true
gives (abridged)
msg: |-
host_1,
host_3,
The next option is to remove blacklisted hosts from the loop, e.g.
blacklist: [host_2]
The template below gives the same result
{% for host in ansible_play_hosts_all|difference(blacklist) %}
{{ host }},
{% endfor %}

Iterate over a YAML object using Jinja template

I have a yaml file with the following contents in it
interfaces:
'loopback:local':
-address: 0.0.0.0
prefix: 24
area: 192.168.1.2
grp: testint
intname: global
-address: 0.0.0.0
prefix: 24
area: 192.168.1.3
grp: prodint
intname: global2
My jinja template looks like this
{% for intf in interfaces %}
{{ intf }}
{% for key,value in intf.items() %}
{{ key }}
{{ value }}
{% endfor %}
{% endfor %}
When I try to access the elements from the object, I get an error saying no elements found
More likely you shoud try something like this:
vars:
interfaces:
- loopback_local:
- address: 0.0.0.0
prefix: 24
area: 192.168.1.2
grp: testint
intname: global
- address: 0.0.0.0
prefix: 24
area: 192.168.1.3
grp: prodint
intname: global2
jinjafile
{% for intf in interfaces %}
{% for i in intf.loopback_local %}
{% for key,value in i.items() %}
{{ key }}:{{ value }}
{% endfor %}
{% endfor %}
{% endfor %}

How can I use AWS tags in a template?

I know that it is possible to loop over lists in a Jinja2 template like this:
{% for host in vars['play_hosts'] %}
"{{ host }}"{% if not loop.last %},{% endif %}
{% endfor %}
(Example stolen from this question.)
How can I use AWS tag based values like "the IPs of all hosts with value 'dev' in tag 'Environment'" instead of vars['play_hosts']?
I found out myself that it is only a matter of using the right group. All ec2 instances with the same tag can be found in a group and can be addressed by using e.g. groups['tag_Environment_dev']. So the example from the question would look like this:
{% for host in groups['tag_Environment_dev'] %}
"{{ host }}"{% if not loop.last %},{% endif %}
{% endfor %}

Ansible jinja2 filter ipaddress

I have 3 servers A, B, C. When I run the below in j2 template, it pulls all 3 servers.
How can I ignore the server that runs the template?
For ex, if server A runs, it should fetch only server B & C.
"retry_join": ["
{% for host in groups['servers']%}
{{ hostvars[host]['ansible_ssh_host'] }}
{% if not loop.last %}, {% endif %}
{% endfor %}
"]
Exclude inventory_hostname from your servers list:
{% for host in groups['servers'] | difference([inventory_hostname]) %}
You can try a check like this one to verify that your loop variable is not the current host:
{% if host != inventory_hostname %}
{{ hostvars[host]['ansible_ssh_host'] }}
{% endif %}

Ansible/Jinja: condition with an undefined statement

I need to iterate over all hosts and generate config file for hosts that are not contained in group somegroup:
{% for host in groups.all if host not in groups['somegroup'] %}
But if somegroup does not exist, it fails (argument of type 'StrictUndefined' is not iterable).
How do I write this correctly to avoid two different for cycles:
{% if groups['somegroup'] is defined %}
{% for host in groups.all if host not in groups['somegroup'] %}
...
{% endfor %}
{% else %}
{% for host in groups.all %}
...
{% endfor %}
{% endif %}
I think you're looking for the default filter:
{% for host in groups.all if host not in groups['somegroup'] | default([]) %}

Resources