PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

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

 August 17, 2022     ansible, ansible-2.x, lint, linux, output     No comments   

Issue

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: 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"
    ]
}

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!

Solution

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



Answered By - AKS
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing