Ansible output formatting options

12,160

Solution 1

I have a plugin to format the output. The gist is here but the gist's gist is:

# Save as <folder with your playbook>/callback_plugins/<some name>.py
# Optionally use no_log: True on your playbook/roles/tasks to suppress other output

import sys
import pprint

class CallbackModule(CallbackBase):

    def log(self, host, category, data):
        pp = pprint.PrettyPrinter(indent=2, stream=sys.stdout)
        pp.pprint(data)

Solution 2

You basically have two choices. One is to do what folks have described above, which is to pipe the output of the ansible command to things like perl, sed, awk, etc.

The second alternative is to write your own Python script that calls ansible directly. Ansible is written in Python and as such you can call it directly from your own Python code. The Ansible documentation on its Python API provides details on how to do this.

Share:
12,160
FRC
Author by

FRC

Updated on June 06, 2022

Comments

  • FRC
    FRC almost 2 years

    Is there an option to format ansible output with the use of ansible (not any other scripts)? For instance

    name: Show version
      sudo: true
      hosts: web_front_end
      tasks:
        - name: Create yum cache
          shell: yum makecache
        - name: Check the version of Portal
          shell: rpm -qa | grep portal
          register: portal
        - debug: msg={{portal.stdout}}
      tags:
        - portal
        - wfe
    

    I would like to get only the

    TASK: [debug msg={{portal.stdout}}]
    

    part. Or even is there a way to get only the shell command output?