Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active October 6, 2017 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/7b31d8f93a5f0656388e0deff9a156eb to your computer and use it in GitHub Desktop.
Save halberom/7b31d8f93a5f0656388e0deff9a156eb to your computer and use it in GitHub Desktop.
ansible - jsonifying yaml
$ ansible-playbook play.yml
PLAY [localhost] ***************************************************************************************************************************************************************************************************************************************************************
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "[{\"foo\": \"bar\", \"abc\": \"xyz\"}, {\"foo\": 123, \"abc\": 456}]"
}
TASK [command] *****************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"result": {
"changed": true,
"cmd": [
"echo",
"\"[{\"foo\": \"bar\", \"abc\": \"xyz\"}, {\"foo\": 123, \"abc\": 456}]\"",
"|",
"python",
"-m",
"json.tool"
],
"delta": "0:00:00.006991",
"end": "2017-10-06 12:35:30.929534",
"failed": false,
"rc": 0,
"start": "2017-10-06 12:35:30.922543",
"stderr": "",
"stderr_lines": [],
"stdout": "\"[{\"foo\": \"bar\", \"abc\": \"xyz\"}, {\"foo\": 123, \"abc\": 456}]\" | python -m json.tool",
"stdout_lines": [
"\"[{\"foo\": \"bar\", \"abc\": \"xyz\"}, {\"foo\": 123, \"abc\": 456}]\" | python -m json.tool"
]
}
}
TASK [copy] ********************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0
$ cat /tmp/foo
[{"foo": "bar", "abc": "xyz"}, {"foo": 123, "abc": 456}]
---
- hosts: localhost
connection: local
gather_facts: False
vars:
mylistofdicts:
- { foo: bar, abc: xyz }
- { foo: 123, abc: 456 }
tasks:
- debug:
msg: "{{ mylistofdicts|to_json }}"
- command: echo '"{{ mylistofdicts|to_json }}"' | python -m json.tool
register: result
- debug: var=result
- copy:
content: "{{ mylistofdicts|to_json }}"
dest: /tmp/foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment