Bootstrap FreeKB - Ansible - Append to a dictionary
Ansible - Append to a dictionary

Updated:   |  Ansible articles

Let's say you create an empty dictionary using vars.

---
- hosts: localhost
  vars:
    food: {}
  tasks:
  - debug: 
      var: food
...

 

Which should return the following.

ok: [localhost] => {
    "food": {}
}

 

Here is how you can append key value pairs to the dictionary. As long as each key is unique, this should work.

---
- hosts: localhost
  vars:
    food: {}
  tasks:
  - set_fact:
      food: "{{ food | combine({'fruit': 'apple'}) }}"

  - set_fact:
      food: "{{ food | combine({'veggy': 'onion'}) }}"

  - debug: 
      var: food
...

 

Or like this.

---
- hosts: localhost
  vars:
    food: {}
  tasks:
  - set_fact:
      food: "{{ food | combine({item.key: item.value}) }}"
    with_items:
    - key: fruit
      value: apple
    - key: veggy
      value: onion

  - debug:
      var: food
...
~    

 

Which should return the following.

ok: [localhost] => {
    "food": {
        "fruit": "apple", 
        "veggy": "onion"
    }
}

 

Here is how you can append key value pairs to the dictionary. However, this will overwrite the value in the "fruit" key, meaning that dictionary will contain a single "fruit" key that contains the last value, "banana" in this example.

---
- hosts: localhost
  vars:
    food: {}
  tasks:
  - set_fact:
      food: "{{ food | combine({'fruit': 'apple'}) }}"

  - set_fact:
      food: "{{ food | combine({'fruit': 'banana'}) }}"

  - debug: 
      var: food
...

 

Similarly, this would overwrite "apple" with "banana".

---
- hosts: localhost
  vars:
    food: {}
      - { fruit: apple }
  tasks:
  - set_fact:
      food: "{{ food | combine({'fruit': 'banana'}) }}"

  - debug: 
      var: food
...

 

Which should return the following.

ok: [localhost] => {
    "food": {
        "fruit": "banana"
    }
}

 

You can append a new key value pair to the dictionary. However, just like in the prior example, the dictionary will be overwritten with the latest key value pair, tomato in this example.

---
- hosts: localhost
  vars:
    food: {}
      - { fruit: apple }
  tasks:
  - set_fact:
      food: "{{ food | combine({'veggie': 'onion'}) }}"

  - set_fact:
      food: "{{ food | combine({'veggie': 'tomato'}) }}"

  - debug: 
      var: food
...

 

Which should return the following.

ok: [localhost] => {
    "food": {
        "fruit": "banana",
        "veggie": "tomato"
    }
}

 

Here is one possible way to append to the list / dictionary without overwriting.

---
- hosts: localhost
  vars:
    food: []
  tasks:
  - name: append to the food list / dictionary
    vars:
      data:
        - { fruit: "{{ item }}" }
    set_fact:
      food: "{{ food | default([]) + [data] }}"
    with_items:
      - apple
      - banana
      - grape
      - orange

  - debug:
      var: food
...

 

Which should return the following.

ok: [localhost] => {
    "food": [
        [
            {
                "fruit": "apple"
            }
        ], 
        [
            {
                "fruit": "banana"
            }
        ], 
        [
            {
                "fruit": "grape"
            }
        ], 
        [
            {
                "fruit": "orange"
            }
        ]
    ]
}

 

Here is how you could append nested keys.

---
- hosts: localhost
  vars:
    food: {}
  tasks:
  - set_fact:
      food: "{{ food | combine({'foods': {'fruit':'apple'}}) }}"

  - debug:
      var: food
...

 

Which should return the following.

ok: [localhost] => {
    "food": {
        "foods": {
            "fruit": "apple"
        }
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


February 22 2024 by Viacheslav
THANKS a lot!!!!

Add a Comment


Please enter d9163f in the box below so that we can be sure you are a human.