skip to Main Content

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?

2

Answers


  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 %}
    
    Login or Signup to reply.
    • 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 %}
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search