How to put the string JSON passed as a parameter into the dictionary when writing Ansible module

tl;dr

import ast

desired_dict = ast.literal_eval(module.params['json_str'])

let's do it.

What's wrong

When writing an Ansible playbook, In many cases, a JSON file is read and passed as a character string.

  tasks:
    - name: Update
      spam_module:
        state: present
        json_str: "{{ lookup('file', 'ham.json') }}"

To read this in the module

argument_spec.update(
        dict(
            json_str=dict(type='str', required=True),
            state=dict(default='present', type='str', choices=['present', 'absent']),
        )
    )

    module = AnsibleModule(argument_spec=argument_spec)

    print(module.params['json_str'])

I can read it by doing something like Double quotes are converted to single quotes in module.params.

At this time, trying to convert to JSON easily fails.

desire_dict = json.loads(module.params['json_str'])
# Fail!!!

What to do

Since the character string enclosed in single quotes can be handled as Abstract Syntax Tree in Python,

desired_dict = ast.literal_eval(module.params['json_str'])

It's a good idea to convert it to a dictionary using ʻast.literal_eval`.

Where to use

I want to compare two JSONs for idempotency check I dropped it in a dictionary and compared it.

However, JSON does not guarantee the order of lists, but dictionaries do guarantee the order, so it is not a strict comparison. Something like that is common in Ansible, so let's do our best.

Recommended Posts

How to put the string JSON passed as a parameter into the dictionary when writing Ansible module
How to connect the contents of a list into a string
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
[Ansible] How to call variables when creating your own module
How to import NoteBook as a module in Jupyter (IPython)
In Django, how to abbreviate the long displayed string as ....
How to import NoteBook as a module in Jupyter (IPython)
How to make a string into an array or an array into a string in Python
[Introduction to Python] How to split a character string with the split function
Precautions when using a list or dictionary as the default argument
How to check the memory size of a dictionary in Python
How to set Jupytext nicely when managing code as a team
How to extract the desired character string from a line 4 commands
How to use the optparse module
How to put a symbolic link
How to use the ConfigParser module
How to input a character string in Python and output it as it is or in the opposite direction.
How to put a line number at the beginning of a CSV file
[Introduction to Python] How to write a character string with the format function
How to manage arguments when implementing a Python script as a command line tool
[AWS] Wordpress How to deal with "The response is not a correct JSON response"
[Python] I tried to get the type name as a string from the type function