在使用以下劇本摘錄檢查我的節點上的埠范圍時,
- 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 }}"
我得到以下示例輸出,其中所有內容都與\t分隔符連接:
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"
]
}
但是用下面的 bash 腳本運行同樣的東西,
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
它輸出以下漂亮的表格:

是否可以使用劇本而不是那些分隔符來獲得這樣的表格輸出\t?
uj5u.com熱心網友回復:
要允許 Ansible 顯示新行 ( \n) 和制表符 ( \t) 等值,您可以使用debug 回呼。
這可以通過修改ansible.cfg來完成,如果你想在整個 Ansible 安裝中應用它,例如
[defaults]
stdout_callback = debug
ansible-playbook或者,以這種方式呼叫:
ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook play.yaml
例如; 給定任務:
- debug:
msg: "some\tfields\tin\ttable\nfoo\tbar\tbaz\tqux"
當使用debug回呼運行時,這會產生:
TASK [debug] ***************************************************************
ok: [node1] => {}
MSG:
some fields in table
foo bar baz qux
在您的具體情況下,您可以做的是:
- debug:
msg: "{{ output.stdout_lines | join('\n') }}"
使用debug回呼會產生:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/420658.html
標籤:
