How to loop through a list of dictionary, and print out every key and value pair in Ansible
<p>I have an list of dictionary in Ansible config</p>

<pre><code>myList
    - name: Bob
      age: 25
    - name: Alice
      age: 18
      address: USA
</code></pre>

<p>I write code as</p>

<pre><code>- name: loop through
  debug: msg ="{{item.key}}:{{item.value}}"
  with_items: "{{ myList }}"
</code></pre>

<p>I want to print out like</p>

<pre><code>msg: "name:Bob age:25 name:Alice age:18 address:USA"
</code></pre>

<p>How can I loop through this dictionary and get key and value pairs? Because it don't know what is the key. If I change as {{ item.name }}, ansible will work, but I also want to know key</p>

<p>Here is your text:
myList
    - name: Bob
      age: 25
    - name: Alice
      age: 18
      address: USA</p>

<p>Here is the Answer:</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>text='''myList
          - name: Bob
            age: 25
          - name: Alice
            age: 18
            address: USA'''</p>
    </blockquote>
  </blockquote>
</blockquote>

<pre><code>&gt;&gt;&gt; final_text=''
</code></pre>

<blockquote>
  <blockquote>
    <blockquote>
      <p>for line in text.split('\n'):
          line1=line.replace(' ','').replace('-','')
          if 'myList' not in line1:
              final_text+=line1+' '</p>
      
      <p>final_text
      'name:Bob age:25 name:Alice age:18 address:USA '</p>
    </blockquote>
  </blockquote>
</blockquote>

python ansible