【发布时间】:2022-01-24 11:08:49
【问题描述】:
在使用以下剧本摘录检查我的节点上的端口范围时,
- 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分隔符来获得这样的表格输出?
【问题讨论】:
-
您使用文字
\t转义看到的结果是 Python 存储字符串的方式;它们与实际输出无关,就像值列表周围的[...,...]分界线一样。如果您以某种方式打印这些字符串,您应该会看到实际的选项卡。 -
顺便说一句,您的命令行中没有任何内容使用任何 Bash 功能。在特定目录中运行它似乎也没有任何好处。
标签: bash shell ansible delimiter