Skip to content

Commit

Permalink
Add os_updates role (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias McNulty <[email protected]>
  • Loading branch information
copelco and tobiasmcnulty authored Feb 15, 2023
1 parent aedd3e3 commit a720d67
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ roles:
src: https://github.com/Oefenweb/ansible-postfix
```

## `hosting_services.os_updates`

Runs operating system updates and reboots the server, if needed.

```yaml
# playbook.yaml
- hosts: all
become: yes
tags: os_updates
roles:
- caktus.hosting_services.os_updates
```

```yaml
# vars file
aws_profile: ""
os_updates_reboot: true
os_updates_salt_hold: false
os_updates_ec2_instances: false
```

## `hosting_services.rsyslog_forwarding`

Forwards logs to an external syslog server via rsyslog.
Expand Down
6 changes: 6 additions & 0 deletions roles/os_updates/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
aws_profile: ""

os_updates_reboot: false
os_updates_salt_hold: false
os_updates_ec2_instances: false
54 changes: 54 additions & 0 deletions roles/os_updates/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
- name: Hold salt packages
shell: "echo {{ item }} hold | sudo dpkg --set-selections"
loop:
- salt-common
- salt-master
- salt-minion
tags: os_updates
when: os_updates_salt_hold

- name: Remove useless packages from the cache
apt:
autoclean: yes
tags: os_updates

# clean out old kernels to make room in /boot before an upgrade
- name: Remove dependencies that are no longer required
apt:
autoremove: yes
tags: os_updates

- name: Run updates
apt:
upgrade: dist
cache_valid_time: 3600
tags: os_updates

- name: Check if a reboot is required
register: needs_reboot
stat:
path: /var/run/
get_md5: no
changed_when: needs_reboot.stat.exists
when: os_updates_reboot

- debug:
msg: "{{ ansible_host }} : scheduled for reboot"
when: os_updates_reboot and needs_reboot.stat.exists

- name: Rebooting
reboot:
msg: "rebooting {{ ansible_host }}"
when: os_updates_reboot and needs_reboot.stat.exists and not os_updates_ec2_instances

# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-reboot.html
- name: Rebooting via Amazon EC2 API
community.aws.ec2_instance:
state: restarted
instance_ids:
- "{{ hostvars[inventory_hostname].instance_id }}"
profile: "{{ aws_profile }}"
become: no
delegate_to: 127.0.0.1
when: os_updates_reboot and needs_reboot.stat.exists and os_updates_ec2_instances

0 comments on commit a720d67

Please sign in to comment.