You can use the reboot or shutdown -r now to reboot the Linux system. However, this doesn’t scale well if you have many Alpine Linux servers (or clusters based upon Alpine Linux) and need to reboot the server only if a new Linux kernel is installed. Let us see how to use the Ansible IT automation tool to reboot the Alpine Linux VM or server only when a new Linux kernel is installed.
nixCraft: Privacy First, Reader Supported
- nixCraft is a one-person operation. I create all the content myself, with no help from AI or ML. I keep the content accurate and up-to-date.
- Your privacy is my top priority. I don’t track you, show you ads, or spam you with emails. Just pure content in the true spirit of Linux and FLOSS.
- Fast and clean browsing experience. nixCraft is designed to be fast and easy to use. You won’t have to deal with pop-ups, ads, cookie banners, or other distractions.
- Support independent content creators. nixCraft is a labor of love, and it’s only possible thanks to the support of our readers. If you enjoy the content, please support us on Patreon or share this page on social media or your blog. Every bit helps.
Finding out Alpine Linux kernel version and name
Run the uname command to find Linux kernel version:# uname -r
Outputs:
6.1.45-0-virt
Your Alpine Linux comes with various Linux kernel packages. Here is how to list them using the apk command and grep command:# apk search -d 'kernel' | grep 'linux-[a-z]*'
The following command will list your currently installed kernel package name:# apk info | grep '^linux'
Outputs:
linux-virt
So I have linux-virt package installed. So I have linux-virt package installed. However, please note that the package name may differ based on the installation process and the preferences of your system administrator or cloud service provider.
Rebooting Alpine Linux machine
You can run the following shell command with pipe and capture status with bash:# set -o pipefail; apk -u list | grep '^linux-virt'; echo$?
The $? shell variable prints the exit status of the last executed command (apk -u list | grep ‘^linux-virt’) by bash. Here is the Ansible code: – name: Run Shell Command with Pipe and Capture Status shell: “set -o pipefail; apk -u list | grep ‘^linux-virt’; echo $?” register: pipe_status_result changed_when: false
We can extract and print it as follows: – name: Extract Exit Status from Result set_fact: pipe_exit_status: “{{ pipe_status_result.stdout_lines[-1] }}” – name: Print the value of the variable “pipe_exit_status” debug: var: pipe_exit_status
If pipe_exit_status is set to 0 (zero), means we can reboot the Alpine Linux.
How to reboot Alpine Linux using Ansible playbook when kernel is updated
Here is a sample Ansible playbook that will upgrade all installed packages and reboot the Alpine Linux machine only if a new Linux kernel installed:— #
— – name: Update pacakges and reboot if a new kernel installed hosts: my_alpine_vms tasks: – name: Update Alpine packages cache community.general.apk: update_cache: true # Change ‘linux-virt’ to ‘linux-lts’/’linux-edge’ kernel package (set correct package name here) # – name: Run Shell Command with Pipe and Capture Status shell: “set -o pipefail; apk -u list | grep ‘^linux-virt’; echo $?” register: pipe_status_result changed_when: false – name: Extract Exit Status from Result set_fact: pipe_exit_status: “{{ pipe_status_result.stdout_lines[-1] }}” – name: Print the value of the variable “pipe_exit_status” debug: var: pipe_exit_status – name: Upgrade Alpine Linux packages now community.general.apk: upgrade: yes – name: Reboot the system if a kernel update is installed reboot: msg: “Reboot initiated by Ansible for kernel updates” connect_timeout: 5 reboot_timeout: 600 pre_reboot_delay: 0 post_reboot_delay: 30 test_command: uptime when: pipe_exit_status == 0
Run your playbook as follows:$ ansible-playbook -i hosts alpine_linux.yaml
## OR ##
$ ansible-playbook -i hosts --ask-vault-pass --extra-vars '@alpine.private.data.yml' alpine_linux.yaml
Add your Alpine Linux IP address or hostnames in your hosts file. The following is sample hosts file and customize as per your needs. For example:[my_alpine_vms:vars] ansible_user=appadmin ansible_become=yes ansible_python_interpreter=‘/usr/bin/python3’ [my_alpine_vms] # Linode DC for hosted VPN GW behind geo DNS ln.sg-vpn-1 ln.mum-vpn-1 ln.atl-vpn-1 # AWS EC2 cluster a and b zone behind ELB ec2-eu-west-2-www-001-a ec2-eu-west-2-www-002-a ec2-eu-west-2-www-003-a ec2-eu-west-2-www-004-a ec2-eu-west-2-www-005-b ec2-eu-west-2-www-006-b ec2-eu-west-2-www-007-b ec2-eu-west-2-www-008-b
Summing up
First, you need to upgrade the Alpine Linux package cache. Then you need to add a condition to the Ansible playbook to check if a new Linux kernel will be installed. If so, then you set the correct variables. Finally, apply the package upgrade and reboot the box if the variable indicates you installed a new Alpine Linux kernel package. You need to do this because the Ansible reboot module has no option to do a conditional reboot.