Ansible - Update values in a dictionary 
                
            
            
            
             
            
            
                           
                
            
            
            
                
    
    
     
            
                
                    by
                    Jeremy Canfield  |  
                    Updated: February 23 2023
                    
                          |  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 