Home:ALL Converter>Ansible: set_fact on a json object

Ansible: set_fact on a json object

Ask Time:2017-01-03T09:16:24         Author:Willem van Ketwich

Json Formatter

I have a json object in an Ansible variable (my_var), which contains values similar to the following:

{
    "Enabled": "true"
    "SomeOtherVariable": "value"
}

I want to modify the value of Enabled in my_var and have tried the following:

set_fact:
  my_var.Enabled: false

and

set_fact:
  my_var['Enabled']: false

Which both give errors similar to:

"The variable name 'my_var.Enabled' is not valid. Variables must start with a letter or underscore character, and contain only letters, numbers and underscores."

Can this be done with set_fact or is there some other way of achieving this?

Author:Willem van Ketwich,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41435169/ansible-set-fact-on-a-json-object
Willem van Ketwich :

this was my solution - probably not the most eloquent:\n\n- set_fact:\n my_temp_enabled_var: '{ \"Enabled\": \"false\" }'\n\n- set_fact:\n my_temp_enabled_var: \"{{ my_temp_enabled_var | from_json }}\"\n\n- set_fact:\n my_var: \"{{ my_var | combine(my_temp_enabled_var) }}\"\n",
2017-01-03T02:07:43
yy