【问题标题】:Ansible Builtin Shell: how can I get a table output instead of "\t" delimiters?Ansible Builtin Shell:如何获得表格输出而不是“\ t”分隔符?
【发布时间】: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


【解决方案1】:

要允许 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多