ansible security automation
Automatische Security Updates mit Ansible
Security Updates sind wichtig. Aber wer hat Zeit, jeden Server täglich zu checken? Ansible + Cron = Automatisierung.
Das Problem
- Server brauchen regelmäßige Updates
- Manuell ist fehleranfällig und zeitaufwändig
- Vergessene Server werden zu Sicherheitsrisiken
Die Lösung
Ein Ansible Playbook das:
- Updates installiert
- Bei Bedarf rebootet
- Logs führt
- Benachrichtigt
Das Playbook
playbooks/security-updates.yml:
---
- name: Security Updates
hosts: all
become: true
serial: "{{ serial_count | default('100%') }}"
vars:
reboot_allowed: true
notify_slack: true
tasks:
- name: Update apt cache
apt:
update_cache: true
cache_valid_time: 3600
- name: Get list of upgradable packages
shell: apt list --upgradable 2>/dev/null | grep -v "Listing" || true
register: upgradable
changed_when: false
- name: Show upgradable packages
debug:
msg: "{{ upgradable.stdout_lines }}"
when: upgradable.stdout_lines | length > 0
- name: Install security updates
apt:
upgrade: safe
register: apt_upgrade
- name: Check if reboot is required
stat:
path: /var/run/reboot-required
register: reboot_required
- name: Reboot if required and allowed
reboot:
msg: "Reboot by Ansible Security Updates"
pre_reboot_delay: 5
post_reboot_delay: 30
reboot_timeout: 300
when:
- reboot_required.stat.exists
- reboot_allowed | bool
- name: Notify about updates
debug:
msg: |
Host: {{ inventory_hostname }}
Packages updated: {{ apt_upgrade.changed }}
Reboot required: {{ reboot_required.stat.exists }}
Reboot performed: {{ reboot_required.stat.exists and reboot_allowed }}
Rolling Updates
Für Produktion: Nicht alle Server gleichzeitig:
- name: Rolling Security Updates
hosts: webservers
become: true
serial: 1 # Ein Server nach dem anderen
tasks:
# ... wie oben
Oder prozentual:
serial:
- 1 # Erst einen
- 25% # Dann 25%
- 100% # Rest
Automatisierung mit Cron
Auf deinem Ansible Control Node:
crontab -e
# Security Updates täglich um 3 Uhr
0 3 * * * cd /home/user/ansible && ansible-playbook playbooks/security-updates.yml >> /var/log/ansible-updates.log 2>&1
Mit Logging
playbooks/security-updates.yml:
- name: Security Updates with Logging
hosts: all
become: true
tasks:
# ... Update Tasks ...
- name: Log update results
lineinfile:
path: /var/log/ansible-updates.log
line: "{{ ansible_date_time.iso8601 }} - {{ inventory_hostname }} - Updates: {{ apt_upgrade.changed }} - Reboot: {{ reboot_required.stat.exists }}"
create: true
delegate_to: localhost
become: false
Slack Benachrichtigung
- name: Notify Slack
uri:
url: "{{ slack_webhook_url }}"
method: POST
body_format: json
body:
text: |
*Security Updates completed*
Hosts: {{ ansible_play_hosts | length }}
Updates installed: {{ ansible_play_hosts | map('extract', hostvars, 'apt_upgrade') | selectattr('changed') | list | length }}
delegate_to: localhost
run_once: true
when: notify_slack | default(false)
Unattended Upgrades (Alternative)
Für komplett automatische Updates ohne Ansible:
- name: Setup unattended-upgrades
hosts: all
become: true
tasks:
- name: Install unattended-upgrades
apt:
name: unattended-upgrades
state: present
- name: Configure unattended-upgrades
copy:
dest: /etc/apt/apt.conf.d/50unattended-upgrades
content: |
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::Automatic-Reboot "false";
Unattended-Upgrade::Mail "admin@example.com";
- name: Enable auto-upgrades
copy:
dest: /etc/apt/apt.conf.d/20auto-upgrades
content: |
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
Mein Setup
Ich kombiniere beides:
- Unattended Upgrades: Security-Updates automatisch täglich
- Ansible wöchentlich: Full
apt upgrademit Kontrolle
# Wöchentlich Sonntags um 4 Uhr
0 4 * * 0 cd ~/ansible && ansible-playbook playbooks/full-update.yml
Tipps
- Staging zuerst: Updates erst auf Staging, dann Prod
- Monitoring: Überwache Server nach Updates
- Backups: Vor Major Updates Snapshot erstellen
- Serial nutzen: Nie alle Prod-Server gleichzeitig
Fazit
Automatische Updates sind ein Muss. Mit Ansible hast du Kontrolle UND Automatisierung. Kein Server bleibt mehr vergessen.