Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Ansible output with Linux tools

Tags:

ansible

I have a silly playbook which just runs a command to get the list of vhost in every host on webserver group. As all vhost are located in /var/www is easy to get the list of webs.

The problem is the way of Ansible returns the info. For example:

ok: [host1] => {
    "var": {
        "out.stdout_lines": [
            "",
            "host1.com"
        ]
    }
}
ok: [host2] => {
    "var": {
        "out.stdout_lines": [
            "",
            "host2.com"
        ]
    }
}

Do you know an easy way to just get the name of the vhosts? Using grep awk or something like that?

like image 649
Rubendob Avatar asked Feb 06 '23 08:02

Rubendob


1 Answers

Dirty way: prepend each line in stdout_lines with some marker (e.g. ANSBLMRK_) before printing, so you have a list if "ANSBLMRK_host2.com", then grep and cut.

Good way: set ANSIBLE_STDOUT_CALLBACK=json and pipe it to jq.

like image 76
Konstantin Suvorov Avatar answered Mar 24 '23 00:03

Konstantin Suvorov