Get ansible facts as csv

adhoc aproach with ansible

Simple aproach without any callback plugins, just ansible, perl and jq

1
2
3
4
5
6
  hostpattern='*elastic*'
  inventory=inventory file or comman seperated list of hosts. if its only one host use host,
  ansible -i $inventory  -m setup -a 'filter=ansible_fqdn,ansible_distribution_major_version'  $hostpatern --one-line  \
        | perl -pne 's/.*SUCCESS => (.*)$/$1/g' \
        | jq -r  '.ansible_facts | "\(.ansible_fqdn) \(.ansible_distribution_major_version)" ' \
        | sort

I do not like the perl in here, that looks fragile to me. I think there should be an "output" plugin, ansible calls them Callback plugins

with the callback plugin enable for ansible commands

There is an callback json plugin, which makes the output computer readable with jq, you can eather configure it in the ansible.cfg file, but for ad hoc commands its easier to just specifiy some environment variables Here i use the json callback plugin, and enable it for ansible, i.e. the adhoc commands:

1
2
3
  export ANSIBLE_CALLBACK_WHITELIST=json
  export ANSIBLE_STDOUT_CALLBACK=json
  export ANSIBLE_LOAD_CALLBACK_PLUGINS=1

then I run ansible and pipe the output through jq to get what I want:

1
2
3
4
5
  ansible -i pseudo_inventory,seperated,hosts,by,comma  -m setup \
          -a 'filter=ansible_fqdn,ansible_distribution_file_variety,ansible_distribution_major_version' \
          '*'  \
      | jq -r \
           '.plays[0].tasks[0].hosts |.[] | "\(.ansible_facts.ansible_fqdn);\(.ansible_facts.ansible_distribution_file_variety);\(.ansible_facts.ansible_distribution_major_version)"'

The filter is described in the documentaion of the setup module

what is the name of the ansible fact i am looking for?

gron, again the json callback plugin and grep are helpful: Pipe the output of ansible to gron , grep for whatever comes to mind:

1
2
3
4
  export ANSIBLE_CALLBACK_WHITELIST=json
  export ANSIBLE_STDOUT_CALLBACK=json
  export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
  ansible -i pseudo-inventory, -m setup '*' |gron |grep default_ipv4

TODO playbook aproach with ansible-playbook

delegating facts

1
2
3
4
5
6
7
8
9
---
- hosts: app_servers

  tasks:
    - name: Gather facts from db servers
      ansible.builtin.setup:
      delegate_to: "{{ item }}"
      delegate_facts: true
      loop: "{{ groups['dbservers'] }}"

accessible as hostvars[‘dbhost1’][‘ansible_default_ipv4’][‘address’]