How to output in different formats using hammer on Red Hat Satellite

Solution Verified - Updated -

Environment

  • Red Hat Satellite 6

Issue

  • How to change the tabular formatting of hammer outputs and display the output in different formats.

Resolution

  • The following output formats are available with hammer:

    • base
    • table
    • silent
    • csv
    • yaml
    • json

    The default output format is table.

    hammer -h 
    
    Options:
    --csv                         Output as CSV (same as --output=csv)
    --csv-separator VALUE         Character to separate the values
    --output ENUM                 Set output format
                                  Possible value(s): 'base', 'table', 'silent', 'csv', 'yaml', 'json'
    --output-file VALUE           Path to custom output file
    --version                     Show version
    
  • To change the output format, specify the output format with --output option. For example, --output json can be used for JSON output.

  • Here are some examples:

    CSV:

    hammer --output csv task list --per-page 10
    ID,Action,State,Result,Started at,Ended at,Duration,Owner,Task errors
    026ca857-0e61-424e-bfc7-2463fcc6eaba,Run Sync Plan: ,scheduled,pending,"","",0,foreman_admin,""
    11f6fd35-c348-45a3-8bc2-09316a9daad9,Subscription expiration notification ,scheduled,pending,"","",0,,""
    15516521-7450-45bc-881a-6519262ac85a,Wait and Inventory scheduled sync ,scheduled,pending,"","",0,foreman_admin,""
    191f2978-91b7-4705-b70b-980b5eb4b3bf,Subscription Manifest expiration date check ,scheduled,pending,"","",0,,""
    31cb74f9-acca-42ad-8672-43a26351b284,Wait and Insights scheduled sync ,scheduled,pending,"","",0,foreman_admin,""
    3550b2d1-cb43-4f36-9fca-fe818ac01e87,Host lifecycle support expiration notification ,scheduled,pending,"","",0,,""
    3b402062-954e-4e94-aa35-77e189dbb3fb,Wait and Generate all reports job ,scheduled,pending,"","",0,foreman_admin,""
    3b4621f4-e938-486c-b8f0-4f818ca3f38b,Pulp disk space notification ,scheduled,pending,"","",0,,""
    43d4983a-aa18-4624-8ac5-4ea80b66326d,Create RSS notifications ,scheduled,pending,"","",0,,""
    7015a6b7-9cab-41cd-887c-25bfb5a417d1,Check for long running tasks ,scheduled,pending,"","",0,foreman_admin,""
    

    For CSV format outputs, --csv option can be used as well. The CSV separator can be changed with the --csv-separator option.

    hammer --csv --csv-separator '|' task list --per-page 10
    ID|Action|State|Result|Started at|Ended at|Duration|Owner|Task errors
    026ca857-0e61-424e-bfc7-2463fcc6eaba|Run Sync Plan: |scheduled|pending|""|""|0|foreman_admin|""
    11f6fd35-c348-45a3-8bc2-09316a9daad9|Subscription expiration notification |scheduled|pending|""|""|0||""
    15516521-7450-45bc-881a-6519262ac85a|Wait and Inventory scheduled sync |scheduled|pending|""|""|0|foreman_admin|""
    191f2978-91b7-4705-b70b-980b5eb4b3bf|Subscription Manifest expiration date check |scheduled|pending|""|""|0||""
    31cb74f9-acca-42ad-8672-43a26351b284|Wait and Insights scheduled sync |scheduled|pending|""|""|0|foreman_admin|""
    3550b2d1-cb43-4f36-9fca-fe818ac01e87|Host lifecycle support expiration notification |scheduled|pending|""|""|0||""
    3b402062-954e-4e94-aa35-77e189dbb3fb|Wait and Generate all reports job |scheduled|pending|""|""|0|foreman_admin|""
    3b4621f4-e938-486c-b8f0-4f818ca3f38b|Pulp disk space notification |scheduled|pending|""|""|0||""
    43d4983a-aa18-4624-8ac5-4ea80b66326d|Create RSS notifications |scheduled|pending|""|""|0||""
    7015a6b7-9cab-41cd-887c-25bfb5a417d1|Check for long running tasks |scheduled|pending|""|""|0|foreman_admin|""
    

    YAML:

    hammer --output yaml task list --per-page 10
    - ID: 026ca857-0e61-424e-bfc7-2463fcc6eaba
      Action: 'Run Sync Plan: '
      State: scheduled
      Result: pending
      Started at:
      Ended at:
      Duration: '0'
      Owner: foreman_admin
    - ID: 11f6fd35-c348-45a3-8bc2-09316a9daad9
      Action: 'Subscription expiration notification '
      State: scheduled
      Result: pending
      Started at:
      Ended at:
      Duration: '0'
      Owner:
    

    JSON:

    hammer --output json content-export list --organization-id 1 --fields "Id,Type,content view version"
    [
     {
       "ID": 1,
       "Type": "complete",
       "Content View Version": "Test 1.0"
     },
     {
       "ID": 2,
       "Type": "complete",
       "Content View Version": "Test 1.0"
     },
     {
       "ID": 3,
       "Type": "complete",
       "Content View Version": "Test 1.0"
     },
     {
       "ID": 4,
       "Type": "complete",
       "Content View Version": "Test 1.0"
     }
    ]
    

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments