Skip to content

Dictionary

Append

ansible.builtin.combine

Append
- name: Dict1
  hosts: localhost
  gather_facts: false

  vars:
    myvalue: "MyValue"

  tasks:
    - name: Append to Dictionary
      ansible.builtin.set_fact:
        dict1: >-
          {{
            dict1 | default({})
            | combine({ "key1" : "value" })
            | combine({ "key2" : myvalue })
          }}

    - name: Display Recursive
      ansible.builtin.debug:
        var: dict1
TASK [Display Append] *************************
ok: [localhost] => {
    "dict1": {
        "key1": "value",
        "key2": "MyValue"
    }
}

Recursive

Append
- name: Dict2
  hosts: localhost
  gather_facts: false

  vars:
    save: "date_of_day"
    day: "monday"
    time: "11"

  tasks:
    - name: Create Dict
      ansible.builtin.set_fact:
        dict2: >-
          {{
            dict2 | default({})
            | combine({  save : { 'jour' : day }}, recursive=True )
            | combine({  save : { 'seq' : time }}, recursive=True )
          }}

    - name: Display Recursive
      ansible.builtin.debug:
        var: dict2
TASK [Display Recursive] ***********************
ok: [localhost] => {
    "dict2": {
        "date_of_day": {
            "jour": "monday",
            "seq": "11"
        }
    }
}

List

ansible.builtin.dict2items

ansible.builtin.items2dict

Longueur

(Nombre de keys) d'un dictionnaire

{{ dict.keys() | list | length | int }}

Extraction (selectattr)

selectattr

JSON
"success_nodes": [
    {
        "identifier": "47301d67-9e86-4ef1-bba3-b4511d3f72ac",
                  "type": "workflow_job_template_node",
                  "workflow_job_template": {
                      "name": "I_WFL_DEPLOY_double",
                    "organization": {
                        "name": "ATOM-Stockage",
                      "type": "organization"
                    },
                    "type": "workflow_job_template"
                  }
                },
                {
                    "identifier": "d25ed6f2-7eef-4ca1-80c6-b45ae87e99e0",
                  "type": "workflow_job_template_node",
                  "workflow_job_template": {
                      "name": "I_WFL_DEPLOY_double",
                    "organization": {
                      "name": "ATOM-Stockage",
                      "type": "organization"
                    },
                    "type": "workflow_job_template"
                  }
                }
              ]

Extraction (selectattr / attribute)

YAML
ListId : "{{ success_nodes | default([], true) | selectattr('identifier', 'defined') | map(attribute='identifier')) | list }}"
Text Only
ListId: [ "47301d67-9e86-4ef1-bba3-b4511d3f72ac", "d25ed6f2-7eef-4ca1-80c6-b45ae87e99e0" ]
 ou si vide
 ListId: []

Combine