1

I am stuck in this piece of work.. i have tried so many things but i think i have gone far into the abyss that i cant think outside now.

I have a a fact with the list:

set_fact is food_sub_list

{
        "food_a": {
            "list_veg": {
                "description": "list veg stuff"
            },
            "some_bar": {
                "description": "bar list"
            }
        },
        "food_b": {
            "veg_abc": {
                "description": "b veg stuff"
            },
            "b_time": {
                "description": "b is the bee"
            }
        }
    }

From this list i want to loop through so each food list has it's own config file. For example for food_a:

food.conf

{% for f in food_sub_list %} (looping through food_a)
{{ f }} << this gives me food_a so this bit works
{{ f.xx? }} << here i want list_bar and some_bar corresponding to food_a which i cant get
{% endfor %}

I cannot get list_veg and some_bar for the above config.

So far i have tried json_query but it doesnt give me just the sub keys.

How can i get keys and value but just the first level without description key and value?

spiceitup
  • 132
  • 10

2 Answers2

1

You can try following:

 {% for f in food_sub_list %}
       {% for key, value in food_sub_list[f].items() %}
           {{ key }} #this will give you second level key
           {{ value }} #this will give you second level value
       {% endfor %}
 {% endfor %}
Binita
  • 26
  • 3
  • Thank you, but this is giving the whole list for both food_a and food_b level. I want to loop through separately food_a first and then food_b. So loop through `food_a` and get key `list_veg` and `some_bar` then next loop goes to `food_b` and get key `veg_abc` and `b_time` – spiceitup Jul 14 '23 at 12:12
1
  • The below template
{% for f,sub in food_sub_list.items() %}
{{ f }}
{{ sub.keys() }}
{% endfor %}

gives

food_a
['list_veg', 'some_bar']
food_b
['b_time', 'veg_abc']
  • Declare the below dictionary result if you want to use JmesPath
  result: "{{ dict(food_sub_list|dict2items|json_query(_query)) }}"
  _query: '[].[key, keys(value)]'

gives

  result:
    food_a: [list_veg, some_bar]
    food_b: [b_time, veg_abc]

Then, the below template gives the same output

{% for f,sub in result.items() %}
{{ f }}
{{ sub }}
{% endfor %}

Example of a complete playbook for testing

- hosts: localhost

  vars:

    food_sub_list:
      food_a:
        list_veg:
          description: list veg stuff
        some_bar:
          description: bar list
      food_b:
        b_time:
          description: b is the bee
        veg_abc:
          description: b veg stuff

    result: "{{ dict(food_sub_list|dict2items|json_query(_query)) }}"
    _query: '[].[key, keys(value)]'

  tasks:

    - debug:
        msg: |
          {% for f,sub in food_sub_list.items() %}
          {{ f }}
          {{ sub.keys() }}
          {% endfor %}

    - debug:
        var: result|to_yaml

    - debug:
        msg: |
          {% for f,sub in result.items() %}
          {{ f }}
          {{ sub }}
          {% endfor %}
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63