从清单中创建字典

时间:2018-07-06 15:42:04

标签: ansible

我得到以下结果,我想创建字典表单列表。你能请教吗?

输入:

  "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"
    ]
]

}

预期输出:

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

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

谢谢, 妈妈

1 个答案:

答案 0 :(得分:1)

好吧,这个工作了。尝试执行以下操作,然后阅读我发送的该链接中的所有帖子...它将按您的需要工作。

  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

您的输出将如下所示:

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

希望这可以为您提供帮助。

来源:Using Ansible set_fact to create a dictionary from register results