creating json inline

An API that I use accepts only JSON. I’m creating what I need with set_fact, which I’ll later use with the uri module. However, the curly braces are getting interpreted as a dictionary or something.

Here’s my playbook:

Escape those braces:

  • set_fact: >
    sshkey_json=‘{“text”: “{{public_key_contents.stdout}}”}’

results in:

TASK: [debug var=sshkey_json] *************************************************
ok: [127.0.0.1] => {
“sshkey_json”: “\{"text": "ssh-rsa blahblah comment"\}”
}

Which I think is what you want.

Hmm, that was what I thought I wanted, but using that value for the API call was refused as it was not valid JSON.

Might be easier to just declare it as YAML.

If you need it as a string, there’s stuff like

{{ foo | to_json }}