Skip to content

Instantly share code, notes, and snippets.

@skamithi
Last active February 22, 2018 19:17
Show Gist options
  • Save skamithi/633eb79be63f596b434a1f32000d49ee to your computer and use it in GitHub Desktop.
Save skamithi/633eb79be63f596b434a1f32000d49ee to your computer and use it in GitHub Desktop.
rhel prevalidate
---
# User Input Variables (extra vars)
# memory_threshold: Value in MB. If free memory is less than this value then stop prevalidation
# swap_threshold: Value in MB. If free swap is less than this value then stop prevalidation
# disk_threshold: Value in MB. If free disk space is less than this value then stop prevalidation
# change selected host group to match requirements
- hosts: all
# ensure gather facts is on
gather_facts: yes
vars:
memory_threshold: 100
swap_threshold: 1000
disk_threshold: 1000
tasks:
- name: "check if memory is below {{ memory_threshold }} MB"
assert:
that: "ansible_memory_mb.real.free > memory_threshold "
msg: "Free memory is {{ ansible_memory_mb.real.free }} MB and is less than the pre-validation threshold of {{ memory_threshold }} MB"
- name: "check if swap is below {{ swap_threshold }} MB"
assert:
that: "ansible_memory_mb.swap.free > swap_threshold"
msg: "Free Swap space is {{ ansible_memory_mb.swap.free }} MB and is less than the pre-validation threshold of {{ swap_threshold }} MB"
- name: convert disk_threshold from Mb to Kb
set_fact:
disk_threshold_in_kb: "{{ disk_threshold * 1024 }}"
- name: check that all disk sizes are not below {{ disk_threshold }} MB"
assert:
that: "item.size_available > disk_threshold_in_kb|int "
msg: "Mountpoint: {{ item.mount }} is less than {{ disk_threshold }} MB available"
with_items: "{{ ansible_mounts }}"
- name: full fstab output
shell: cat /etc/fstab
register: full_fstab_output
changed_when: false
- name: capture etc/fstab output
shell: awk '!/^#/ { print $2 }' /etc/fstab
register: fstab_output
changed_when: false
- name: set fstab entries into a variable called fstab_mountpoints.
set_fact:
fstab_mountpoints: "{{ fstab_output.stdout_lines | difference(['','swap']) }}"
- name: get list of mount points
set_fact:
devices_mounted: "{{ ansible_mounts | map(attribute='mount') | list }}"
- name: get a list of fstab mounts that are not active
set_fact:
mounts_not_active: "{{ fstab_mountpoints | difference(devices_mounted) }}"
- name: fail if not all fstab mounts are not active
assert:
that: "mounts_not_active == []"
msg: "Some mounts in /etc/fstab are not active - mount point list {{ mounts_not_active }}"
- name: use yum check-updates because yum-utils may be not installed on these RHEL systems
shell: yum check-updates -q
register: yum_updates
failed_when: yum_updates.rc != 100
changed_when: false
- name: list packages that need to be updated in the package_updates variable
set_fact:
package_updates: "{{ yum_updates.stdout_lines | difference(['']) }}"
- name: commands in this block require escalated privileges
block:
- name: get last yum command run, start , end date and installed packages
shell: yum history info | grep -e "Begin time" -e "Command Line" -e "Installed" -e "User"
changed_when: false
register: last_yum_command
- name: clean all yum caches. Must be a become user to execute this
shell: yum clean all
changed_when: false
become: true
- name: grab df output for printing to the report
shell: df -Ph
register: df_output
changed_when: false
# Generate a report. Recommend using email as the notification mechanism or
# copying to a SCP/NFS share for further review.
# In this example, report is copied to the ansible server- not recommended. Just doing it for demo purposes.
# If using Ansible Tower use the recommended notification mechanism instead.
- name: generate a report
template:
src: prevalidation-report.j2
dest: /tmp/{{inventory_hostname}}_prevalidation-report.txt
- name: |
fetch the report and place on the local ansible server.
Do not do this step if running this task from Ansible Tower.
It will break the Tower Project repository Sync process
fetch:
src: /tmp/{{inventory_hostname}}_prevalidation-report.txt
dest: "./{{inventory_hostname}}_prevalidation-report.txt"
flat: yes
PRE-VALIDATION REPORT
=====================
Hostname: {{ inventory_hostname }}
Kernel Version: {{ ansible_kernel }}
Packages to Update: {{ package_updates |length }}
Disk Layout and Sizing:
------------------------
{{ df_output.stdout }}
/etc/fstab
----------
{{ full_fstab_output.stdout }}
Last Yum Command
---------------
{{ last_yum_command.stdout }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment