Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Created December 17, 2023 11:40
Show Gist options
  • Save zeitounator/bba819e9efa103f9f46480c5e74d1a80 to your computer and use it in GitHub Desktop.
Save zeitounator/bba819e9efa103f9f46480c5e74d1a80 to your computer and use it in GitHub Desktop.
$ tree
.
├── fake_command
├── test_as_json_default_stdout.yml
└── test_as_lines_yaml_stdout.yml
0 directories, 3 files
$ cat fake_command
#!/usr/bin/env bash
cat <<EOF
{
"db" : "efendibey",
"collections" : 15,
"views" : 0,
"objects" : 389,
"avgObjSize" : 463.32647814910024,
"dataSize" : 180234,
"storageSize" : 479232,
"indexes" : 15,
"indexSize" : 405504,
"totalSize" : 884736,
"scaleFactor" : 1,
"fsUsedSize" : 39113961472,
"fsTotalSize" : 42256773120,
"ok": 1
}
EOF
$ ./fake_command
{
"db" : "efendibey",
"collections" : 15,
"views" : 0,
"objects" : 389,
"avgObjSize" : 463.32647814910024,
"dataSize" : 180234,
"storageSize" : 479232,
"indexes" : 15,
"indexSize" : 405504,
"totalSize" : 884736,
"scaleFactor" : 1,
"fsUsedSize" : 39113961472,
"fsTotalSize" : 42256773120,
"ok": 1
}
$ cat test_as_
test_as_json_default_stdout.yml test_as_lines_yaml_stdout.yml
$ cat test_as_json_default_stdout.yml
---
- hosts: localhost
gather_facts: false
tasks:
- name: Ask for db detail (with fake command returning your exact output)
ansible.builtin.command:
cmd: ./fake_command
chdir: "{{ playbook_dir }}"
register: whatever
- name: Show result from above as json output
ansible.builtin.debug:
var: whatever.stdout | from_json
$ ansible-playbook test_as_json_default_stdout.yml
PLAY [localhost] ***************************************************************************************************************
TASK [Ask for db detail (with fake command returning your exact output)] *******************************************************
changed: [localhost]
TASK [Show result from above as json output] ***********************************************************************************
ok: [localhost] => {
"whatever.stdout | from_json": {
"avgObjSize": 463.32647814910024,
"collections": 15,
"dataSize": 180234,
"db": "efendibey",
"fsTotalSize": 42256773120,
"fsUsedSize": 39113961472,
"indexSize": 405504,
"indexes": 15,
"objects": 389,
"ok": 1,
"scaleFactor": 1,
"storageSize": 479232,
"totalSize": 884736,
"views": 0
}
}
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ cat test_as_lines_yaml_stdout.yml
---
- hosts: localhost
gather_facts: false
tasks:
- name: Ask for db detail (with fake command returning your exact output)
ansible.builtin.command:
cmd: ./fake_command
chdir: "{{ playbook_dir }}"
register: whatever
# !!! This will only give the exepected result with community.general.yaml
- name: Show result from above as a list of lines
ansible.builtin.debug:
var: whatever.stdout_lines
$ ANSIBLE_STDOUT_CALLBACK=community.general.yaml ansible-playbook test.yml
ERROR! the playbook: test.yml could not be found
$ ANSIBLE_STDOUT_CALLBACK=community.general.yaml ansible-playbook test_as_lines_yaml_stdout.yml
PLAY [localhost] ***************************************************************************************************************
TASK [Ask for db detail (with fake command returning your exact output)] *******************************************************
changed: [localhost]
TASK [Show result from above as a list of lines] *******************************************************************************
ok: [localhost] =>
whatever.stdout_lines:
- '{'
- ' "db" : "efendibey",'
- ' "collections" : 15,'
- ' "views" : 0,'
- ' "objects" : 389,'
- ' "avgObjSize" : 463.32647814910024,'
- ' "dataSize" : 180234,'
- ' "storageSize" : 479232,'
- ' "indexes" : 15,'
- ' "indexSize" : 405504,'
- ' "totalSize" : 884736,'
- ' "scaleFactor" : 1,'
- ' "fsUsedSize" : 39113961472,'
- ' "fsTotalSize" : 42256773120,'
- ' "ok": 1'
- '}'
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment