最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dictionary - Ansible: Parsing JSON data to a variable - Stack Overflow

programmeradmin2浏览0评论

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') }}"

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') }}"
Share Improve this question edited Jan 30 at 10:17 U880D 12.3k6 gold badges33 silver badges67 bronze badges asked Jan 29 at 18:24 AnsirockAnsirock 11 bronze badge 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. – U880D Commented Jan 30 at 10:40
Add a comment  | 

1 Answer 1

Reset to default 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
发布评论

评论列表(0)

  1. 暂无评论