Skip to content

Commit 876f988

Browse files
committed
Add save_ansible_facts.yml playbook for saving facts "in-tree"
1 parent dabc7f8 commit 876f988

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

docs/usage.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ First, generate Ansible output for your hosts:
77
mkdir out
88
ansible -m setup --tree out/ all
99

10+
In case you want to generate Ansible facts output in your existing
11+
playbooks, you can use/include the
12+
[save_ansible_facts.yml](https://github.com/fboender/ansible-cmdb/blob/master/example/playbooks/save_ansible_facts.yml)
13+
playbook which is provided in the examples.
14+
1015
Next, call ansible-cmdb on the resulting `out/` directory to generate the CMDB
1116
overview page:
1217

@@ -401,4 +406,3 @@ Generate the overview:
401406
./ansible-cmdb out/ out_custom/ > overview.html
402407

403408
The software items will be listed under the "*Custom facts*" heading.
404-
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
# .. vim: foldmarker=[[[,]]]:foldmethod=marker
3+
4+
- name: Save Ansible facts
5+
hosts: [ 'all_managed_hosts' ]
6+
become: False
7+
gather_facts: False
8+
9+
tasks:
10+
11+
- name: Write Ansible facts to facts directory
12+
command: ansible -m setup --tree {{ inventory_facts_path|d((inventory_dir | realpath) + "/../../facts") | quote }} {{ inventory_hostname }} --become
13+
become: False
14+
delegate_to: 'localhost'
15+
changed_when: False
16+
17+
- name: Remove often changing facts to make facts deterministic and suitable to be put under version control
18+
shell: jq '{{ inventory_facts_jq_filter|d("del(.ansible_facts.ansible_date_time, .ansible_facts.ansible_env.SUDO_COMMAND, .ansible_facts.ansible_uptime_seconds, .ansible_facts.ansible_memfree_mb, .ansible_facts.ansible_memory_mb, .ansible_facts.ansible_env.SSH_CLIENT, .ansible_facts.ansible_local.etckeeper.commit)") }}' --sort-keys {{ (inventory_hostname) | quote }} > {{ (inventory_hostname + ".tmp") | quote }} && mv {{ (inventory_hostname + ".tmp") | quote }} {{ (inventory_hostname) | quote }};
19+
args:
20+
chdir: '{{ inventory_facts_path|d((inventory_dir | realpath) + "/../../facts") }}'
21+
become: False
22+
delegate_to: 'localhost'
23+
changed_when: False

0 commit comments

Comments
 (0)