0

Starting off with Ansible and I am trying to use ReST API to interact with an external application.Maybe I am missing something simple here.

I am trying to compare every host in my inventory file with the POD name specified in the variable file used by the role that invokes the jinja2 template.

My inventory file looks like this:

    [all]
    'POD-9'
    'POD-10' 

Variable file :

    pods:
        params:
        - name: POD-9
        - name: POD-10


    {% for pod in pods.params %}
    {% if '{{ inventory_hostname }}' == '{{ pod.name }}' %} 
    <generate JSON template here>
    {% endif %}
    {% endfor %}

The if statement however does not take effect. I want the template to be generated only in the inventory_hostname is equal to the pod name in the variable file

The current JSON file includes both : { "pod": { "name": "POD-9" } "pod": {
"name": "POD-10" } }

Dee
  • 1
  • 1

2 Answers2

2

In Jinja2 the double curly braces are used as a print statement. If you access variables inside tags don’t put the braces around them

{% for pod in pods.params %}
    {% if inventory_hostname == pod.name %} 
        <generate JSON template here>
    {% endif %}
{% endfor %}
r-m-n
  • 14,192
  • 4
  • 69
  • 68
0

Found the problem : {% if pod.name == inventory_hostname %}

Dee
  • 1
  • 1