-1

I have Ansible output from a task which is registered to task_output.

"ansible_loop_var": "item",
"changed": false,
"files": [
   {
       "path": "file/path",
}

I tried to captured the path but none of there are working

  "{{ task_output.files | map(attribute='path') }}"
  "{{ task_output[0].files[0] | map(attribute='path') }}"
  "{{ task_output[0].files | map(attribute='path') }}"
  "{{ task_output.files[0] | map(attribute='path') }}"
  "{{ task_output.json.files | map(attribute='path') }}"
  "{{ task_output | map(attribute='path') }}"
3
  • > task_output.files[0].path or task_output.files.0.path, even. Commented Jan 29 at 18:36
  • To map, you need a list, but by getting the element 0, you don't have a list anymore, instead, you have a dictionary. Commented Jan 29 at 18:37
  • The data structure files": [ { "path": "file/path", } does not looks valid to me. Commented Jan 30 at 10:40

1 Answer 1

0

For a list with a single element a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    files: [
      {
        "path": "file/path",
      }
    ]

  tasks:

    - debug:
        var: (files | first).path

    - debug:
        var: files | map(attribute='path')

will result into an output of

TASK [debug] ********************
ok: [localhost] =>
  (files | first).path: file/path

TASK [debug] ********************
ok: [localhost] =>
  files | map(attribute='path'):
  - file/path
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.