Created
August 3, 2015 11:26
-
-
Save halberom/e276aed3e99ae7df143c to your computer and use it in GitHub Desktop.
ansible - example of using from_json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PLAY [localhost] ************************************************************** | |
GATHERING FACTS *************************************************************** | |
ok: [localhost] | |
TASK: [shell cat '/tmp/file.json'] ******************************************** | |
changed: [localhost] | |
TASK: [debug var=result] ****************************************************** | |
ok: [localhost] => { | |
"var": { | |
"result": { | |
"changed": true, | |
"cmd": "cat '/tmp/file.json'", | |
"delta": "0:00:00.003907", | |
"end": "2015-08-03 12:25:29.650127", | |
"invocation": { | |
"module_args": "cat '/tmp/file.json'", | |
"module_name": "shell" | |
}, | |
"rc": 0, | |
"start": "2015-08-03 12:25:29.646220", | |
"stderr": "", | |
"stdout": "{\n \"foo\": \"bar\",\n \"bob\": [\n \"one\",\n \"two\"\n ],\n \"fox\": {\n \"jumped\": \"over\",\n \"the\": \"lazy\"\n }\n}", | |
"stdout_lines": [ | |
"{", | |
" \"foo\": \"bar\",", | |
" \"bob\": [", | |
" \"one\",", | |
" \"two\"", | |
" ],", | |
" \"fox\": {", | |
" \"jumped\": \"over\",", | |
" \"the\": \"lazy\"", | |
" }", | |
"}" | |
], | |
"warnings": [] | |
} | |
} | |
} | |
TASK: [set_fact myvar="{{ result.stdout | from_json }}"] ********************** | |
ok: [localhost] | |
TASK: [debug var=myvar] ******************************************************* | |
ok: [localhost] => { | |
"var": { | |
"myvar": { | |
"bob": [ | |
"one", | |
"two" | |
], | |
"foo": "bar", | |
"fox": { | |
"jumped": "over", | |
"the": "lazy" | |
} | |
} | |
} | |
} | |
PLAY RECAP ******************************************************************** | |
localhost : ok=5 changed=1 unreachable=0 failed=0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
connection: local | |
tasks: | |
- shell: cat '/tmp/file.json' | |
register: result | |
- debug: var=result | |
- set_fact: myvar="{{ result.stdout | from_json }}" | |
- debug: var=myvar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"foo": "bar", | |
"bob": [ | |
"one", | |
"two" | |
], | |
"fox": { | |
"jumped": "over", | |
"the": "lazy" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This helped me, thank you!