Patching your Ubuntu server is essential for several reasons:
Ansible is an open-source automation tool that simplifies:
Key features:
Ensures uniform updates across all servers
Update multiple servers simultaneously
Reduces risk of human error
Tailor playbooks to your specific needs
On your control machine, run:
sudo apt update && sudo apt install ansible -y
Set up SSH keys for passwordless login:
ssh-keygen -t rsa
ssh-copy-id user@server-ip
Create a hosts
file:
[webservers]
server1 ansible_host=192.168.1.10 ansible_user=ubuntu
server2 ansible_host=192.168.1.11 ansible_user=ubuntu
[dbservers]
server3 ansible_host=192.168.1.12 ansible_user=ubuntu
Create a YAML file for updating Ubuntu servers:
---
- hosts: all
become: yes
tasks:
- name: Update apt cache
apt: update_cache=yes
- name: Upgrade all packages
apt: upgrade=dist
- name: Auto-remove unused packages
apt: autoremove=yes
- name: Reboot if necessary
reboot:
msg: "Reboot initiated by Ansible"
connect_timeout: 5
reboot_timeout: 600
pre_reboot_delay: 0
post_reboot_delay: 30
test_command: whoami
Execute the playbook with this command:
ansible-playbook -i hosts update-ubuntu.yml
This tells Ansible to run the tasks defined in the playbook on the servers listed in your inventory file.
After running the playbook, verify the updates:
Always test in a staging environment first.
Ensure reliable backup strategy before updates.
Implement monitoring to detect post-update issues.
Schedule regular updates to stay current.
While Ansible is excellent for managing updates, LinuxPatch.com can further streamline the process: