Rubendob
Rubendob

Reputation: 1694

Parsing Ansible output with Linux tools

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?

Upvotes: 4

Views: 5257

Answers (2)

Rob H
Rob H

Reputation: 1716

Maybe just write a file containing the hosts names in your playbook and then use that later:

tasks:

- name: make output file
  file: name=./list_of_hosts state=touch

- name: show my hostname
  lineinfile: dest=./list_of_hosts line="{{ item }}"
  with_items:
    "{{ out.stdout_lines[1] }}"

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68239

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.

Upvotes: 8

Related Questions