Ansible was designed to remotely manage multiple Linux servers simultaneously. Scripts can be used for many common tasks like updating, rebooting, or to the check health of your Linux servers . Ansible scripts are very format sensitive, so be sure that all columns match up, as below, or it will not run.
Notice that some scripts call sudo and you need the ‘-K’ switch in the command. You can tell if the script calls sudo by the line ‘become: yes’.
Update & Reboot all Servers
This will updated, upgrade, remove unnecessary files, and clear the local repository cache. Finally, it will send an email when completed. It will run against all servers, in the ini file, listed in the group called ‘all_servers’.
# Ansible script, update.yml
# Update all servers.
# cmd: ansible-playbook -K -i inventory.ini update.yml
---
- name: Update Servers
hosts: all_servers
gather_facts: no
become: true
tasks:
- name: Run apt update
apt:
update_cache: yes
- name: Run apt upgrade.
apt:
name: "*"
state: latest
- name: Run apt autoremove.
apt:
autoremove: yes
- name: Run apt autoclean. Clean the /var/cache/apt/archive file
apt:
autoclean: yes
- name: Reboot servers.
reboot:
- name: Send email that task was completed.
hosts: localhost
gather_facts: false
become: false
tasks:
- mail:
host: exchange.company.com
port: 25
from: hostname@company.com
to: user1@company.com
subject: Servers have been updated
body: All servers have been successfully updated and rebooted.
delegate_to: localhost
run_once: True
Reboot Specific Servers
# Ansible script, reboot.yml
# Use to reboot multiple specific servers.
# cmd: ansible-playbook -K -i inventory.ini reboot.yml
---
- name: Reboot specified servers
hosts: server01.company.com:server02.company.com:server03company.com
gather_facts: no
become: true
tasks:
- name: Reboot servers.
reboot:
Check the Health of the Remote Servers
The check health script gathers basic information about the remote servers. Is the hard disk drive full? Does the server need a reboot?
The ansible script calls a bash script, that is then executed on all remote hosts. The results are returned and printed to a text file. An email copies the contents of the text file to the body of the email and results are emailed. Be sure to save the inventory.ini, bash scripts, and the ansible scripts in the same directory.
# Ansible script, health.yml
# Get hostname, uptime, reboot state, & disc space of each server.
# cmd: ansible-playbook -i inventory.ini health.yml
---
- name: Run health check of servers.
hosts: all_servers
gather_facts: false
tasks:
- name: Get health of security servers
script: /home/user1/svr_health.sh
ignore_errors: False
register: results
- name: Make a header for a txt file.
shell: echo '<--- SERVERS DAILY REPORT --->'
register: title
delegate_to: localhost
run_once: true
- name: Create a new txt file.
local_action: copy content={{ title.stdout }} dest=/home/user1/svr_health.txt
- name: Append results to txt file.
lineinfile:
dest: /home/user1/svr_health.txt
line: "{{ results.stdout }}"
insertafter: EOF
delegate_to: localhost
- name: Sending email.
mail:
host: exchange.company.com
port: 25
from: hostname@company.com
to:
- user1@company.com
- user2@company.com
subject: Servers Health Status
body: "Expected Server Cnt: 18 \nActual Total Cnt: {{ ansible_play_hosts | length }}\n\n {{ lookup('file','/home/user1/svr_health.txt') }}"
delegate_to: localhost
run_once: True
#!/bin/bash
date=`date`
uptime=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $2" "$3 }'`
hostname=`hostname`
file="/var/run/reboot-required"
# Note: Assumes all servers have a logical volume drive.
diskspace=`df -h | grep /dev/mapper/`
echo
echo "Date: $date"
echo "Hostname: $hostname"
echo "Space: $diskspace"
echo "Uptime: $uptime"
if [[ -f $file ]];then
echo "Reboot: *** Reboot Required ***"
else
echo "Reboot: No Reboot Required"
fi