Ansible: printing out JSON first level keys names
<p>Example :</p>

<pre><code>{
        "fw1": {
                "ipv4": {
                        "rtr": {

                                "ip": "1.2.3.4",
                                "net": "1.2.3.4",

                        }
                }
        },
        "fw2": {
                "ipv4": {
                        "rtr": {
                                "ip": "4.3.2.1",
                                "net": "4.3.2.1",

                        }
                }
        }
}
</code></pre>

<p>I need to list the first level keys of a json file.<br> Using 'from_json).keys()' i get  a strange syntax -->
 <strong>[u'fw1', u'fw2'].</strong>
<br>where do the <strong>'u'</strong> characters come from and how to get rid of them ? Is there any way to list the keys instead of getting them in an Array ?</p>

<p>You don't need to use <code>from_json</code> here:</p>

<pre><code>---
- hosts: localhost
  gather_facts: True
  vars:
    my_json: "{{ lookup('file','test.json') }}"
  tasks:
    - debug:
        msg: "Keys list: {{ my_json.keys() }}"
</code></pre>

python json ansible ansible-playbook