Ansible 内置 Shell:如何获得 table 输出而不是“\t”分隔符?
Ansible Builtin Shell: how can I get a table output instead of "\t" delimiters?
在使用以下剧本摘录检查我的节点上的端口范围时,
- 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
它输出如下漂亮的table:
是否可以使用剧本而不是那些 \t
分隔符来获得这样的 table 输出?
要允许 Ansible 显示新行 (\n
) 和制表符 (\t
) 等值,您可以使用 debug
callback.
这可以通过修改 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
在使用以下剧本摘录检查我的节点上的端口范围时,
- 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
它输出如下漂亮的table:
是否可以使用剧本而不是那些 \t
分隔符来获得这样的 table 输出?
要允许 Ansible 显示新行 (\n
) 和制表符 (\t
) 等值,您可以使用 debug
callback.
这可以通过修改 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