Skip to content

Instantly share code, notes, and snippets.

@halberom
Created August 3, 2015 11:26
Show Gist options
  • Save halberom/e276aed3e99ae7df143c to your computer and use it in GitHub Desktop.
Save halberom/e276aed3e99ae7df143c to your computer and use it in GitHub Desktop.
ansible - example of using from_json
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
---
- 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
{
"foo": "bar",
"bob": [
"one",
"two"
],
"fox": {
"jumped": "over",
"the": "lazy"
}
}
@anuthaharinimn
Copy link

how to print all the environment variables form docker container as a json and POST that to some API using ansible:

Thanks in advance

@Axellurcher
Copy link

This helped me, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment