Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save_ansible_facts.yml playbook for saving facts "in-tree" #168

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ First, generate Ansible output for your hosts:
mkdir out
ansible -m setup --tree out/ all

In case you want to generate Ansible facts output in your existing
playbooks, you can use/include the
[save_ansible_facts.yml](https://github.com/fboender/ansible-cmdb/blob/master/example/playbooks/save_ansible_facts.yml)
playbook which is provided in the examples.

Next, call ansible-cmdb on the resulting `out/` directory to generate the CMDB
overview page:

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

The software items will be listed under the "*Custom facts*" heading.

32 changes: 32 additions & 0 deletions example/playbooks/save_ansible_facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# .. vim: foldmarker=[[[,]]]:foldmethod=marker

- name: Save Ansible facts
hosts: [ 'all' ]
become: False
gather_facts: False

tasks:

- name: Ensure local facts directory exists
file:
state: 'directory'
path: '{{ inventory_facts_path|d((inventory_dir | realpath) + "/../../facts") }}'
become: False
delegate_to: 'localhost'

- name: Write Ansible facts to facts directory
command: ansible -m setup --tree {{ inventory_facts_path|d((inventory_dir | realpath) + "/../../facts") | quote }} {{ inventory_hostname }} --become
args:
chdir: '{{ ((inventory_dir|realpath) + "/../..") | quote }}'
become: False
delegate_to: 'localhost'
changed_when: False

- name: Remove often changing facts to make facts deterministic and suitable to be put under version control
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 }};
args:
chdir: '{{ inventory_facts_path|d((inventory_dir | realpath) + "/../../facts") }}'
become: False
delegate_to: 'localhost'
changed_when: False