-2

I have following results and I would like to create dictionary form lists. Could you please advise?

Input:

  "destination": [
    [
        "URL=http://apps.introext.net/meta.html,checksum=99e2714f6269cbe49fe641ab9f4f84e661334fa4,remotefile=meta.html"
    ],
    [
        "URL=http://apps.introext.net/data.html,checksum=7985e6b97a915ec6681b628c783fa2a52c6f055a,remotefile=data.html"
    ]
]

}

Expected Output:

{"URL":"http://apps.introext.net/meta.html", "checksum":"99e2714f6269cbe49fe641ab9f4f84e661334fa2","remotefile":"meta.html"}

{"URL":"http://apps.introext.net/data.html","checksum":"7985e6b97a915ec6681b628c788522a52c6f055a","remotefile":"data.html"}

Thanks, Maddy

Maddy M
  • 23
  • 1
  • 5
  • 1
    Hi Kelson, This is different from actual link provided. I need to extract values from each sub list. – Maddy M Jul 06 '18 at 17:36

1 Answers1

2

Well, got this working from it. Try do the following and then read all the posts from that link I sent... It will work as you need.

  tasks:
    - set_fact:
        first_list: "{{ first_list | default([]) + [item.split(',')] }}"
      with_items: "{{ destination }}"

    - set_fact:
        final_list: "{{ final_list | default([]) | combine(dict([ item.partition('=')[::2]|map('trim')])) }}"
      with_items: "{{ first_list }}"

    - debug: var=final_list

Your output will be something like this:

TASK [debug] *********************************************************************************************************
ok: [localhost] => {
    "final_list": {
        "URL": "http://apps.introext.net/data.html", 
        "checksum": "7985e6b97a915ec6681b628c783fa2a52c6f055a", 
        "remotefile": "data.html"
    }
}

Hope this can help you.

Source: Using Ansible set_fact to create a dictionary from register results

Kelson Silva
  • 512
  • 2
  • 9