Created
July 18, 2024 09:51
-
-
Save bdmorin/57999d9030dde43bd5e06c3d47157276 to your computer and use it in GitHub Desktop.
ansible shutdoen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ansible-playbook -i your_inventory_file schedule_shutdown_and_reboot.yml | |
```yaml | |
--- | |
- name: Schedule VM shutdown and server reboot | |
hosts: kvm_servers | |
become: yes | |
vars: | |
shutdown_time: "23:00" | |
tasks: | |
- name: Create a cron job to shutdown VMs and reboot server | |
cron: | |
name: "Shutdown VMs and reboot server" | |
minute: "{{ shutdown_time.split(':')[1] }}" | |
hour: "{{ shutdown_time.split(':')[0] }}" | |
job: "/usr/bin/ansible-playbook /path/to/shutdown_vms_and_reboot.yml" | |
- name: Shutdown VMs and reboot server | |
hosts: kvm_servers | |
become: yes | |
tasks: | |
- name: Gather list of running VMs | |
command: virsh list --name | |
register: running_vms | |
- name: Shutdown running VMs | |
command: "virsh shutdown {{ item }}" | |
loop: "{{ running_vms.stdout_lines }}" | |
when: item != "" | |
- name: Wait for VMs to shutdown | |
command: "virsh list --name" | |
register: vms_after_shutdown | |
until: vms_after_shutdown.stdout_lines == [] | |
retries: 10 | |
delay: 30 | |
- name: Reboot the server | |
reboot: | |
reboot_timeout: 3600 | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment