Reputation: 20039
I currently gather facts about all my hosts using this command:
ansible all -m setup --tree out
This creates a file for each host in the directory out
with all the ansible variables in JSON format. Very useful.
However, my hosts consist of a lot of KVM hosts, so I want to add the output of virt
/ list_vms
to each output.
I created a small playbook:
hosts: myhost
tasks:
- name: VM list checker
virt:
name: list the VMs
command: list_vms
I run this playbook like this:
ansible-playbook -v status.playbook.yml -s
I would like the output to be in JSON
format, preferably combining the facts
and the output of the list_vms
.
How can I create a similar layout (one JSON
per host in the out
directory?) with the combined information?
Upvotes: 3
Views: 4723
Reputation: 20039
Using the input from @KonstantinSuvorov I came up with this:
---
- hosts: kvm_hosts
gather_facts: no
tasks:
- setup:
register: setup_res
- virt: "command=list_vms"
register: cmd_res
- copy:
content: "{{ setup_res | combine(cmd_res) | to_nice_json }}"
dest: /tmp/out/{{ inventory_hostname }}.json
delegate_to: localhost
Upvotes: 4
Reputation: 68269
You can do something like this:
---
- hosts: all
gather_facts: no
tasks:
- setup:
register: setup_res
- command: echo ok
register: cmd_res
- file:
path: /tmp/out/{{ inventory_hostname }}
state: directory
delegate_to: localhost
- copy:
content: "{{ setup_res | to_nice_json }}"
dest: /tmp/out/{{ inventory_hostname }}/facts.json
delegate_to: localhost
- copy:
content: "{{ cmd_res | to_nice_json }}"
dest: /tmp/out/{{ inventory_hostname }}/cmd.json
delegate_to: localhost
replace command
call with virt
.
Upvotes: 2