vijayedm
vijayedm

Reputation: 27

How to display the output of the bash script with newlines

I have a bash script to check some info that need to execute via ansible

file ~/Scripts/test/OpenSSL_p.sh

#!/bin/bash
echo    "HOSTNAME            : $(uname -n)"
echo -e "OS Type             : $(if [[ -e /usr/bin/lsb_release ]];then echo "$(lsb_release -d|cut -d$'\t' -f 2)";elif [[ -e /etc/SuSE-release ]];then echo "$(cat /etc/SuSE-release | tr "\n" " " | cut -f1 -d"#")";else echo "$(cat /etc/redhat-release)";fi)"
echo -e "OpenSSL Ver         : $(/usr/bin/openssl version)"
echo    "Packages            :"
rpm -qa | grep "^openssl-"

ansible playbook

---
- name: precheck
  hosts: all
  tasks:
  - script: ~/Scripts/test/OpenSSL_p.sh
    register: results
  - debug:
      var: results.stdout

I am able execute the script but but output like this,

TASK [debug] ************************************************************************************************************************ok: [nfsserver01] => {
    "results.stdout": "HOSTNAME            : nfscluster1\r\nOS Type             : SUSE Linux Enterprise Server 15 SP2\r\nOpenSSL Ver         : OpenSSL 1.1.1d  10 Sep 2019\r\nPackages            :\r\nopenssl-1.1.1d-1.46.noarch\r\nopenssl-1_1-1.1.1d-11.38.1.x86_64\r\n"
}

I am looking a output like this

HOSTNAME            : nfscluster1
OS Type             : SUSE Linux Enterprise Server 15 SP2
OpenSSL Ver         : OpenSSL 1.1.1d  10 Sep 2019
Packages            :
openssl-1.1.1d-1.46.noarch
openssl-1_1-1.1.1d-11.38.1.x86_64

could you please help me to get this ?

Upvotes: 0

Views: 118

Answers (1)

Kristian
Kristian

Reputation: 3461

Ansible does not render newlines in it's output, it replaces them with \n and \r instead.

However, the debug module can render lists so you could just use results.stdout_lines instead.

- debug:
    var: results.stdout_lines

Would produce:

"results.stdout_lines": [
    "HOSTNAME            : nfscluster1",
    "OS Type             : SUSE Linux Enterprise Server 15 SP2",
    "OpenSSL Ver         : OpenSSL 1.1.1d  10 Sep 2019",
    "Packages            :",
    "openssl-1.1.1d-1.46.noarch",
    "openssl-1_1-1.1.1d-11.38.1.x86_64",
    ""
]

PS. This only applies to the console output, if you intend to save this to a file for example use results.stdout instead.

Upvotes: 1

Related Questions