Skip to content
Advertisement

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

Ansible version: 2.8.3 or Any

I’m using -m <module> Ansible’s ad-hoc command to ensure the following package is installed –OR– let’s say if I have a task to install few yum packages, like (i.e. How can I do the same within a task (possibly when I’m not using ansible’s shell / command modules):

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

It works, but how can I get the output of whole yum operation in a nice output format (instead of getting a one line format with bunch of n characters embedded in it); like what we usually get when we run the same command (yum install <some_package>) on Linux command prompt.

I want to ansible to retain a command’s output in original format

The line I want to see in more linted/beautified way is: 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: fastestmirrornLoading mirror speeds from cached hostfilen * base: mirror.netsite.dkn * elrepo: mirrors.xservers.ron * epel: fedora.mirrors.telekom.ron * extras: centos.mirrors.telekom.ron * remi-php70: remi.schlundtech.den * remi-safe: remi.schlundtech.den * updates: centos.mirror.iphh.netnResolving Dependenciesn--> Running transaction checkn---> Package ncdu.x86_64 0:1.14-1.el7 will be installedn--> Finished Dependency ResolutionnnDependencies Resolvednn================================================================================n Package         Arch              Version                Repository       Sizen================================================================================nInstalling:n ncdu            x86_64            1.14-1.el7             epel             51 knnTransaction Summaryn================================================================================nInstall  1 PackagennTotal download size: 51 knInstalled size: 87 knDownloading packages:nRunning transaction checknRunning transaction testnTransaction test succeedednRunning transactionn  Installing : ncdu-1.14-1.el7.x86_64                                       1/1 n  Verifying  : ncdu-1.14-1.el7.x86_64                                       1/1 nnInstalled:n  ncdu.x86_64 0:1.14-1.el7                                                      nnComplete!n"
    ]
}

Example of the aligned linted/beautified format that I’m looking for, is shown below if I execute: # yum install ncdu, we need need the exact same output, but how it’s per line and easy to read/visualize on 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!

Advertisement

Answer

Few ways:

Inside Ansible’s (2.5 onwards) configuration file ansible.cfg file (either global, in /etc/ansible/ansible.cfg, or a local one in your playbook/project), we can add the following lines under the [defaults] section:

[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

At command line, we can pass the following ENV variable:

ANSIBLE_STDOUT_CALLBACK=debug ansible or ansible-playbook ...

If using a task as shown below, and if I registered the variable in a task, then I can just use:

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

Then, I can use:

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

and if the output of a given Ansible module is other than stdout/debug i.e. in XML or JSON, then in Ansible, configuration file (as listed above), you can enable the appropriate setting by uncommenting i.e.

#stdout_callback = debug
#stdout_callback = json

More info:

debug plugin

anstomlog plugin

YAML plugin

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement