Reputation: 434
I have a file that I want to print as debug msg line.
cat result.txt
## BEGIN :## Role Name: Deployment Checks
## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.
## END :## Role Name: Deployment Checks ##
## BEGIN :## Role Name: Describe the instance
## REASON:- ERROR: Describe the instance FATAL: TEST FAILED.
## END :## Role Name: Describe the instance ##
I m using the below code to print them on the console
- name: Read result
shell: "cat result.txt | grep 'REASON:-'"
register: result
- name: print checks fail.
debug:
msg:
- "Check Failed!"
- "{{ result.stdout_lines }}"
Which prints below output in an ugly format as single line
TASK [ print checks fail.] **************************************************
fatal: [10.203.116.90]: FAILED! => {"changed": false, "msg": ["Check Failed!", ["REASON:- ERROR: Deployment Checks output from command", "REASON:- ERROR: Describe the instance "]]}
How can I print every reason as a single line? So that output is more readable. Something like below
TASK [ print checks fail.] **************************************************
fatal: [10.203.116.90]: FAILED! => {"changed": false, "msg":
["Check Failed!",
["REASON:- ERROR: Deployment Checks output from command",
"REASON:- ERROR: Describe the instance "]]}
Upvotes: 7
Views: 12542
Reputation: 39314
Without a change of callback, you could do:
- debug:
msg: "{{ ['Check Failed!'] + result.stdout_lines }}"
Which will render in
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": [
"Check Failed!",
"## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.",
"## REASON:- ERROR: Describe the instance FATAL: TEST FAILED."
]
}
But as pointed by @U880D's answer, the YAML callback is way more convenient to format output properly.
For example, with the YAML output, you could do:
- debug:
msg: |-
Check Failed!
{{ result.stdout }}
Which will render in
TASK [debug] *******************************************************************
ok: [localhost] =>
msg: |-
Check Failed!
## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.
## REASON:- ERROR: Describe the instance FATAL: TEST FAILED.
Upvotes: 7
Reputation: 12142
You may have a look into Callback plugins and configure it in ansible.cfg
in example with stdout_callback: yaml
.
By using this
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Read result
shell:
cmd: "grep 'REASON:-' result.txt"
register: result
- name: Show result
debug:
msg: "{{ result.stdout_lines }}"
the result would be
TASK [Show result] ***************************************************************
ok: [localhost] =>
msg:
- '## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.'
- '## REASON:- ERROR: Describe the instance FATAL: TEST FAILED.'
as required.
According your example it seems that you have currently configured stdout_callback: json
, and according Ansible Issue #76556 it can't be specified in the playbook level.
Further Q&A
Ansible Documentation
Upvotes: 1