0

I'm trying to access the elements of a list, but whenever I try to access it's elements they are treated as text (see the debug output).

How can I loop over the elements of content[0]?

ansible version: 2.5.1

playbook.yml:

- hosts: localhost
  gather_facts: no
  vars:
    content: []
  tasks:
    - name: Call the Consul API to get all registered services
      uri:
        url: "https://{{API_URL}}/v1/health/service/{{item}}?Status=critical"
        headers:
          Authorization: "Bearer {{TOKEN}}"
        method: GET
        return_content: yes
        status_code: 200
        body_format: json
        force: yes
      register: r
      with_items:
        - service1
        - service2
    - set_fact:
        content: "{{ lookup('flattened', r | json_query('results[*].content')) }}"
    - debug:
        msg: "{{ content[0][0] | type_debug }}"
    - debug:
        msg:  "{{ item | type_debug  }}"
      with_items: "{{ content[0] }}"

debug output:

TASK [debug]
ok: [127.0.0.1] => {
    "msg": "dict"
}

TASK [debug]
ok: [127.0.0.1] => (item=None) => {
    "msg": "AnsibleUnsafeText"
}
Soteri
  • 327
  • 4
  • 21

1 Answers1

0

I'm hoping this question and answer will help you. I think the approach you are taking is the problem.

moonmoon
  • 110
  • 10