Skip to content

Instantly share code, notes, and snippets.

@cinnion
Last active October 19, 2024 20:21
Show Gist options
  • Save cinnion/d00ee1011639e0519800295f57dd21bd to your computer and use it in GitHub Desktop.
Save cinnion/d00ee1011639e0519800295f57dd21bd to your computer and use it in GitHub Desktop.
An ansible playbook and shell script wrapper (for cron) to gather facts, including what virtualization guests might be running on a virtualization host. (And yes, the quotes might be wonky on the debug output at the moment).
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
export ANSIBLE_GATHERING=implicit
script -f -c 'time ansible-playbook --key-file /root/.ssh/id_rsa_ansible /etc/ansible/playbooks/gather-facts.yml' /tmp/ansible-gather-facts.log
---
- hosts: all
pre_tasks:
- name: Make sure /etc/ansible/facts.d exists
file:
path: /etc/ansible/facts.d
state: directory
- name: Push facts files
copy:
src: /etc/ansible/facts.d/firewalld.fact
dest: /etc/ansible/facts.d/
when:
- (ansible_facts['distribution'] in [ 'CentOS', 'RedHat', 'Rocky' ] and ansible_facts['distribution_major_version']|int >= 7) or
(ansible_facts['distribution'] in [ 'Fedora' ] and ansible_facts['distribution_major_version']|int >= 28)
- inventory_hostname != 'myserver'
tasks:
- name: Initial Debug
debug:
msg: "Host {{inventory_hostname}}'s virtualization role is \\\"{{ansible_facts['virtualization_role']}}\\\""
- name: Refresh local ansible facts
setup:
filter: 'ansible_local'
register: mycmd
- name: Debug refreshed vars
debug:
var: mycmd
verbosity: 1
- name: Get guest virtual machines
virt:
command: list_vms
register: list_vms
when:
- ansible_facts['virtualization_role'] == "host"
- inventory_hostname != 'workstation'
- inventory_hostname != 'nas'
- name: Save guest virtual hosts as cachable fact
set_fact:
cacheable: yes
guest_vms: "{{ list_vms.list_vms }}"
when:
- ansible_facts['virtualization_role'] == "host"
- inventory_hostname != 'workstation'
- inventory_hostname != 'nas'
- name: Create groups for virtual host type/role.
group_by:
key: virt_{{ ansible_virtualization_type }}_{{ ansible_virtualization_role }}
when:
"ansible_facts['virtualization_type'] is defined and ansible_facts['virtualization_type'] not in ['', 'NA']"
- name: Create group based on OS family/version for RHEL/CentOS/Rocky
group_by:
key: el{{ ansible_distribution_major_version }}_{{ ansible_architecture }}
parents:
- el{{ ansible_distribution_major_version }}
when:
ansible_facts['os_family'] == 'RedHat' and ansible_facts['distribution'] != 'Fedora'
- name: Debug
vars:
msg: |
Module Variables ("vars"):
--------------------------------
{{ vars | to_nice_json }}
Environment Variables ("environment"):
--------------------------------
{{ environment | to_nice_json }}
GROUP NAMES Variables ("group_names"):
--------------------------------
{{ group_names | to_nice_json }}
GROUPS Variables ("groups"):
--------------------------------
{{ groups | to_nice_json }}
HOST Variables ("hostvars"):
--------------------------------
{{ hostvars | to_nice_json }}
debug:
msg: "{{ msg.split('\n') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment