Parsing ansible output in shell script using sed/grep

9,007

Solution 1

You can print the result of the playbook as a json then parse it in modern language like Python. All you need to do is set an environment variable ANSIBLE_STDOUT_CALLBACK=json

Example:

ANSIBLE_STDOUT_CALLBACK=json ansible-playbook -i hosts.ini main.yaml 

Solution 2

What you need to do is forget using sed or awk on Ansible log, which would become enormously complicated, and instead develop or customise a callback plugin.

Most likely you should start with the example json.py which outputs the Ansible log in JSON format (at least theoretically, as the example code does not seem to work out-of-the-github).

More on developing and configuration of callback plugins in the official Ansible docs.

Share:
9,007

Related videos on Youtube

A.K.G
Author by

A.K.G

Updated on September 18, 2022

Comments

  • A.K.G
    A.K.G over 1 year

    I have a shell script which executes an ansible playbook and I want to process the output of this playbook. I am not sure how can I do this.

    Script:

    #!/bin/sh
    ansible-playbook -i inventory/ec2.py services_status.yml
    

    The output of ansible-playbook command is:

    PLAY [all] *********************************************************************
    
    TASK [cmx_running_services] ****************************************************
    ok: [172.31.35.225]
    
    ok: [172.31.9.253]
    
    
    TASK [debug] *******************************************************************
    ok: [172.31.35.225] => {
        "services": {
            "changed": false,
            "meta": {
                "services": [
                    "zk",
                    "kafka"
                ]
            }
        }
    }
    
    ok: [172.31.9.253] => {
        "services": {
            "changed": false,
            "meta": {
                "MyService": [
                    "default"
                ],
                "services": [
                    "monitoring-agent"
                ]
            }
        }
    }
    
    
    PLAY RECAP *********************************************************************
    172.31.35.225              : ok=2    changed=0    unreachable=0    failed=0
    172.31.9.253               : ok=2    changed=0    unreachable=0    failed=0
    

    In my script I want to process this output and store a json object in format:

    {
      "172.31.35.225":{
        "services":[
          "zk",
          "kafka"
        ]
      },
      "172.31.9.253":{
        "MyService":[
          "default"
        ],
        "services":[
          "monitoring-agent"
        ]
      }
    }