While checking a port range on my nodes with following playbook extract,
- name: bash commands
ansible.builtin.shell: |
grep -E '\b(500[0-9]|50[1-9][0-9]|5[1-4][0-9]{2}|5500)' \
/etc/services | sort -k 2
args:
chdir: /root
executable: /bin/bash
async: 2000
poll: 3
register: output
- debug: msg="{{ output.stdout_lines }}"
- debug: msg="{{ output.stderr_lines }}"
I am getting below sample output, where everything is concatenated with \t delimiters:
ok: [sea_r] => {
"msg": [
"enbd-cstatd\t5051/tcp\t\t\t# ENBD client statd",
"enbd-sstatd\t5052/tcp\t\t\t# ENBD server statd",
"sip\t\t5060/tcp\t\t\t# Session Initiation Protocol",
"sip\t\t5060/udp",
"sip-tls\t\t5061/tcp",
"sip-tls\t\t5061/udp",
"pcrd\t\t5151/tcp\t\t\t# PCR-1000 Daemon",
"xmpp-client\t5222/tcp\tjabber-client\t# Jabber Client Connection",
"xmpp-server\t5269/tcp\tjabber-server\t# Jabber Server Connection",
"cfengine\t5308/tcp",
"mdns\t\t5353/udp\t\t\t# Multicast DNS",
"noclog\t\t5354/tcp\t\t\t# noclogd with TCP (nocol)",
"noclog\t\t5354/udp\t\t\t# noclogd with UDP (nocol)",
"hostmon\t\t5355/tcp\t\t\t# hostmon uses TCP (nocol)",
"hostmon\t\t5355/udp\t\t\t# hostmon uses UDP (nocol)",
"postgresql\t5432/tcp\tpostgres\t# PostgreSQL Database"
]
}
But running the same thing with below bash script,
for r in "${IPS[@]}"; do
ssh -tt root@"$r" "
grep -E --color=always '\b(500[0-9]|50[1-9][0-9]|5[1-4][0-9]{2}|5500)' \
/etc/services | sort -k 2
echo -e "continue to next node"; read
"
done
it outputs the following nice table:
Is it possible to get such a table output with the playbook instead of those \t delimiters?
CodePudding user response:
To allow Ansible to display values like new lines (\n) and tabs (\t), you can use the debug callback.
This can be done, either modifying the ansible.cfg, if you want to apply it on your whole Ansible installation, e.g.
[defaults]
stdout_callback = debug
Or, invoking ansible-playbook this way:
ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook play.yaml
For example; given the task:
- debug:
msg: "some\tfields\tin\ttable\nfoo\tbar\tbaz\tqux"
When run with the debug callback, this yields:
TASK [debug] ***************************************************************
ok: [node1] => {}
MSG:
some fields in table
foo bar baz qux
In your specific case, what you can, then, do, is:
- debug:
msg: "{{ output.stdout_lines | join('\n') }}"
Which would yield, with the usage of the debug callback:
TASK [debug] ***************************************************************
ok: [node1] => {}
MSG:
enbd-cstatd 5051/tcp # ENBD client statd
enbd-sstatd 5052/tcp # ENBD server statd
sip 5060/tcp # Session Initiation Protocol
sip 5060/udp
sip-tls 5061/tcp
sip-tls 5061/udp
pcrd 5151/tcp # PCR-1000 Daemon
xmpp-client 5222/tcp jabber-client # Jabber Client Connection
xmpp-server 5269/tcp jabber-server # Jabber Server Connection
cfengine 5308/tcp
mdns 5353/udp # Multicast DNS
noclog 5354/tcp # noclogd with TCP (nocol)
noclog 5354/udp # noclogd with UDP (nocol)
hostmon 5355/tcp # hostmon uses TCP (nocol)
hostmon 5355/udp # hostmon uses UDP (nocol)
postgresql 5432/tcp postgres # PostgreSQL Database

