Ansible: creating variables from json string

In Ansible, is there a way to convert a dynamic list of key / value pairs that are in a JSON variable to variable names / values ​​that can be accessed in the Playbook without using the file system?

IE. If I have the following JSON in a variable (in my case already imported from a URI call):

{
        "ansible_facts": {
            "list_of_passwords": {
                "ansible_password": "abc123",
                "ansible_user": "user123",
                "blue_server_password": "def456",
                "blue_server_user": "user456"
            }
}

      

Is there a way to convert this JSON variable to equivelant from:

vars:
  ansible_password: abc123
  ansible_user: user123
  blue_server_password: def456
  blue_server_user: user456

      

I usually have to write the variable to a file and then import it using vars_files:

. Our goal is not to write secrets to the file system.

+3


source to share


1 answer


You can use the uri module to make the call and then register the response to the variable:

For example:



- uri:
    url: http://www.mocky.io/v2/59667604110000040ec8f5c6
    body_format: json
  register: response
- debug:
    msg: "{{response.json}}"
- set_fact: {"{{ item.key }}":"{{ item.val }}"}
  with_dict: "{{response.json.ansible_facts.list_of_passwords}}"

      

+4


source







All Articles