0
votes

I have a YAML file with variables that I would like to use in an Ansible template. The file looks like this:

---
config:
  version: "4.0"
  description: "Global configuration"
  sites:
  - name: "Site J"
    description: "Ficititious Site J"
    servers:
    - hostname: server1
    - hostname: server2
    - hostname: server3
  - name: "Site K"
    description: "Ficititious Site K"
  - name: "Site L"
    description: "Ficititious Site L"

I'd trying to reference the servers, but, for the life of me can't figure out the syntax in a Jinja2 template. Here's my current template:

{{ config | to_nice_json }}

{% for site in config['sites'] %}
{{ site['name'] }}
{{ site['description'] }}

{% endfor %}

And the output is:

{
    "description": "Global configuration", 
    "sites": [
        {
            "description": "Ficititious Site J", 
            "name": "Site J", 
            "servers": [
                {
                    "hostname": "server1"
                }, 
                {
                    "hostname": "server2"
                }, 
                {
                    "hostname": "server3"
                }
            ]
        }, 
        {
            "description": "Ficititious Site K", 
            "name": "Site K"
        }, 
        {
            "description": "Ficititious Site L", 
            "name": "Site L"
        }
    ], 
    "version": "4.0"
} 

Site J
Ficititious Site J

Site K
Ficititious Site K

Site L
Ficititious Site L

My problem is accessing the data under "servers:" within the existing site loop. The most obvious syntax for a nested loop:

{% for server in site['servers'] %}
{{ server['hostname'] }}
{% endfor %}

doesn't work. I get this Ansible error:

fatal: [localhost] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict' object has no attribute 'servers'", 'failed': True}

What's the correct syntax for looping through the servers in the YAML file under each of the sites? I've tried many variations and can't seem to find the right one.

2

2 Answers

1
votes

Been working on this for several hours. Trying to understand nested dictionaries and lists, and dictionary keys that are lists, with some success. For someone not experienced in Python, it can be difficult to work with a complicated nested structure in Jinja2. Anyway, here's what I came up with for a template that achieves the goal I was seeking. The template includes a lot of code to help me understand the structures involved. It was a learning experience for me. I have a feeling this could be done in much less code, but this is the best I could do. If anyone can see a way to shorten or otherwise improve the template, that would be great.

{#
  Top-level "config" element contains a dictionary 
#}
{%- for config_key,config_value in config.iteritems() -%}

{%- if config_key == "sites" -%}

{%- set sites_list = config_value -%}

{# 
  Under config, "sites" is a list. 
#}
{%- for site_dict in sites_list -%}

{# 
  Each element of the sites list is a dictionary 
#}
{%- for site_key,site_value in site_dict.iteritems() -%}

{# 
  The dictionary with key="servers" has a list as its value 
#}
{%- if site_key == "servers" -%}

{%- set servers_list = site_value -%}
{%- for server_dict in servers_list -%}

{# 
  Each list value is a dictionary 
#}
{%- for server_key,server_value in server_dict.iteritems() -%}

{#
  Looking for values where the key is "hostname"
#}
{%- if server_key == "hostname" -%}
{{ server_value }}
{% endif %}

{%- endfor -%}

{%- endfor -%}

{%- endif -%}

{%- endfor -%}

{%- endfor -%}

{%- endif -%}

{%- endfor -%}

With this input file:

---
config:
  version: "4.0"
  description: "Global configuration"
  sites:
  - name: "Site J"
    description: "Ficititious Site J"
    servers:
    - hostname: server1
    - hostname: server2
    - hostname: server3
  - name: "Site K"
    description: "Ficititious Site K"
  - name: "Site L"
    description: "Ficititious Site L"

The template produces this output:

server1
server2
server3
0
votes

The servers key doesn't exist in all of the sites, so you need to either skip the loop when it's undefined, or use an empty default list, eg:

{% for server in site['servers'] | default([]) %}
{{ server['hostname'] }}
{% endfor %}