Bootstrap FreeKB - Ansible - Update values in a dictionary
Ansible - Update values in a dictionary

Updated:   |  Ansible articles

Let's say the "food" dictionary contains two key value pairs using the vars plugin.

---
- hosts: all
  vars:
    food: 
      - { key: 'fruit', value: 'apple' }
      - { key: 'veggy', value: 'onion' }

 

The debug module can be used to output the dictionary.

- name: "output the contents of the 'food' hash"
  debug: 
    var: food

 

Which should return the following.

TASK [output the contents of the 'food' hash]
ok: [server1.example.com] => {
    "food": [
        {
            "key": "fruit",
            "value": "apple"
        },
        {
            "key": "veggy",
            "value": "onion"
        },
    ]
}

 

Let's say you want to update apple to banana. The simplest way to get this done is to redefine the dictionary value using the set_fact module.

- set_fact:
    food: 
      - { key: 'fruit', value: 'banana' }

 

 




Did you find this article helpful?

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



Comments


Add a Comment


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