如何获取任何命令/任务(例如:Ansible - Yum Install)/stdout 输出,即漂亮的打印或 Beautify / Lint 对齐输出

How to get any command / task (ex: Ansible - Yum Install ) / stdout output i.e. Pretty print or Beautify / Lint aligned output

Ansible 版本:2.8.3 或任何

我正在使用 -m <module> Ansible 的 ad-hoc 命令来确保安装以下包 -- 或-- 假设我有一个安装几个 yum 包的任务,比如(即我如何在一个任务中做同样的事情(可能当 我没有使用 ansible 的 shell / 命令 模块):

  - name: Installing necessary yum dependencies
    yum:
      name:
        - wget
        - python-devel
        - openssl-devel
      state: latest

有效, 但是我怎样才能以一种很好的输出格式获得整个 yum 操作的输出(而不是使用一堆 \n 嵌入其中的字符);就像我们在 Linux 命令提示符下 运行 相同的命令 (yum install <some_package>) 时通常得到的结果。

我想以原始格式保留命令的输出

我更想看到的linted/beautified方式是:Loaded plugins: ...

[root@ansible-server test_ansible]# ansible -i hosts all -m yum -a 'name=ncdu state=present'
host1 | SUCCESS => {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [


"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirror.netsite.dk\n * elrepo: mirrors.xservers.ro\n * epel: fedora.mirrors.telekom.ro\n * extras: centos.mirrors.telekom.ro\n * remi-php70: remi.schlundtech.de\n * remi-safe: remi.schlundtech.de\n * updates: centos.mirror.iphh.net\nResolving Dependencies\n--> Running transaction check\n---> Package ncdu.x86_64 0:1.14-1.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package         Arch              Version                Repository       Size\n================================================================================\nInstalling:\n ncdu            x86_64            1.14-1.el7             epel             51 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 51 k\nInstalled size: 87 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : ncdu-1.14-1.el7.x86_64                                       1/1 \n  Verifying  : ncdu-1.14-1.el7.x86_64                                       1/1 \n\nInstalled:\n  ncdu.x86_64 0:1.14-1.el7                                                      \n\nComplete!\n"
    ]
}

我正在寻找的对齐 linted/beautified 格式的示例,如果我执行,如下所示:# yum install ncdu,我们需要完全相同的输出,但它是如何每行和容易 read/visualize 在 stdout.

Loaded plugins: amazon-id, langpacks, product-id, search-disabled-repos, subscription-manager

This system is registered with an entitlement server, but is not receiving updates. You can use subscription-manager to assign subscriptions.


*** WARNING ***
The subscription for following product(s) has expired:
  - Oracle Java (for RHEL Server)
  - Red Hat Ansible Engine
  - Red Hat Beta
  - Red Hat CodeReady Linux Builder for x86_64
  - Red Hat Container Images
  - Red Hat Container Images Beta
  - Red Hat Developer Tools (for RHEL Server)
  - Red Hat Developer Tools Beta (for RHEL Server)
  - Red Hat Developer Toolset (for RHEL Server)
  - Red Hat Enterprise Linux Atomic Host
  - Red Hat Enterprise Linux Atomic Host Beta
  - Red Hat Enterprise Linux Server
  - Red Hat Enterprise Linux for x86_64
  - Red Hat Software Collections (for RHEL Server)
  - Red Hat Software Collections Beta (for RHEL Server)
  - dotNET on RHEL (for RHEL Server)
  - dotNET on RHEL Beta (for RHEL Server)
You no longer have access to the repositories that provide these products.  It is important that you apply an active subscription in order to resume access to security and other critical updates. If you don't have other active subscriptions, you can renew the expired subscription.

Resolving Dependencies
--> Running transaction check
---> Package ncdu.x86_64 0:1.15.1-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================
 Package                                                    Arch                                                         Version                                                             Repository                                                  Size
==============================================================================================================================================================================================================================================================
Installing:
 ncdu                                                       x86_64                                                       1.15.1-1.el7                                                        epel                                                        52 k

Transaction Summary
==============================================================================================================================================================================================================================================================
Install  1 Package

Total download size: 52 k
Installed size: 88 k
Is this ok [y/d/N]: y
Downloading packages:
ncdu-1.15.1-1.el7.x86_64.rpm                                                                                                                                                                                                           |  52 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ncdu-1.15.1-1.el7.x86_64                                                                                                                                                                                                                   1/1
  Verifying  : ncdu-1.15.1-1.el7.x86_64                                                                                                                                                                                                                   1/1

Installed:
  ncdu.x86_64 0:1.15.1-1.el7

Complete!

如果您正在寻找任何解决方案,只需搜索您需要的内容并使用 printf 即可完成您想要的操作 - 字符串是“美化的”,它只是用 [=14= 标记新行]:

ansible -i hosts all -m yum -a 'name=ncdu state=present' | grep Loaded | xargs printf

如果您有多个结果行:

ansible -i hosts all -m yum -a 'name=ncdu state=present'  | grep Loaded | while read -r line; do printf "$line"; done

当然,这不会检查结果是否成功,但不清楚您是否也需要它。要打印所有内容(包括很好的结果部分):

ansible -i hosts all -m yum -a 'name=ncdu state=present' | while IFS= read -r line; do printf "$line\n"; done

因为不包含 \n 的所有内容都将正常打印。 IFS= 将保留缩进。

几种方式:

Ansible 内部(2.5 以上)配置文件 ansible.cfg 文件(在 /etc/ansible/ansible.cfg 中的全局文件,或者 [=46= 中的 本地文件]),我们可以在[defaults]部分下添加如下几行:

[defaults]
nocows = True

# Use the debug/yaml/json callback plugins as necessary for your output type.
stdout_callback = debug 
#stdout_callback = debug
#stdout_callback = json

# Use the stdout_callback when running ad-hoc commands.
bin_ansible_callbacks = True

在命令行中,我们可以传递以下ENV变量:

ANSIBLE_STDOUT_CALLBACK=debug ansible or ansible-playbook ...

如果使用如下所示的 task,并且我在任务中注册了变量,那么我可以只使用:

  - name: Installing necessary yum dependencies
    yum:
      name:
        - wget
        - python-devel
        - openssl-devel
      register: yum_std_out
      state: latest

那么,我可以使用:

- debug:
    msg: "{{ yum_std_out.split('\n') }}"

并且如果给定 Ansible 模块的输出不是 stdout/debug,即在 XML 或 JSON 中,那么在 Ansible 中,配置文件(如上所列),您可以通过取消注释启用适当的设置,即

#stdout_callback = debug
#stdout_callback = json

更多信息:

debug plugin

anstomlog plugin

YAML plugin