ansible/playbooks/vhost_reboot.yml
James Antill f74cd17a23 Add regexp to drop spaces from hostnames in prompts, for add_host.
Signed-off-by: James Antill <james@and.org>
2025-03-25 20:36:02 -04:00

148 lines
4.4 KiB
YAML

#
# This playbook lets you safely reboot a virthost and all it's guests.
#
# requires --extra-vars="target=somevhost fqdn"
# Might add nodns=true or nonagios=true to the extra vars
# General overview:
# talk to the vhost
# get back list of instances
# add each of their hostnames to an addhoc group
# halt each of them in a second play
# wait for them to die
# third play, reboot the vhost
# wait for vhost to come back
# TODO: Figure out how to compare virt info pre and post boot.
---
- name: find instances
vars_prompt:
- name: target
prompt: What is the target vhost (to reboot)
private: false
# Use a simple regexp to ignore spaces people might accidentally paste.
hosts: "{{ target | regex_search('[-a-z0-9.]+')}}"
gather_facts: false
user: root
tasks:
- name: get list of guests
virt: command=list_vms state=running
register: vmlist
# - name: get info on guests (prereboot)
# virt: command=info
# register: vminfo_pre
- name: add them to myvms_new group
local_action: add_host hostname={{ item }} groupname=myvms_new
with_items: "{{ vmlist.list_vms }}"
- name: add the vmhost to target group
# Use a simple regexp to ignore spaces people might accidentally paste.
local_action: add_host hostname={{ target | regex_search('[-a-z0-9.]+') }} groupname=target
# Call out to another playbook. Disable any proxies that may live here
- name: update proxy dns if needed
import_playbook: update-proxy-dns.yml
vars:
status: enable
proxies: myvms_new:&proxies
when: nodns is not defined or not "true" in nodns
- name: halt instances
hosts: myvms_new
user: root
gather_facts: false
serial: 1
tasks:
- name: schedule regular host downtime
nagios: action=downtime minutes=30 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
delegate_to: noc01.iad2.fedoraproject.org
ignore_errors: true
when: nonagios is not defined or not nonagios
- name: shutdown vms
virt: command=shutdown name={{ inventory_hostname }}
ignore_errors: true
delegate_to: "{{ hostvars[groups['target'][0]]['ansible_fqdn'] }}"
- name: wait for the whole set to die.
hosts: myvms_new
gather_facts: false
user: root
tasks:
- name: wait for them to die
local_action: wait_for port=22 delay=30 timeout=300 state=stopped host={{ inventory_hostname }}
- name: reboot vhost
hosts: "target"
gather_facts: false
user: root
tasks:
- name: tell nagios to shush
nagios: action=downtime minutes=60 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
delegate_to: noc01.iad2.fedoraproject.org
ignore_errors: true
when: nonagios is not defined or not nonagios
# With drive firmware and sometimes needed to poke things, make this 30m
- name: reboot the virthost
reboot: reboot_timeout=1800
- name: wait for libvirtd to come back on the virthost
wait_for: path=/run/libvirt/libvirt-sock state=present
- name: look up vmlist
virt: command=list_vms
register: newvmlist
- name: add them to myvms_postreboot group
local_action: add_host hostname={{ item }} groupname=myvms_postreboot
with_items: "{{ newvmlist.list_vms }}"
# - name: sync time
# ansible.builtin.command: ntpdate -u 1.rhel.pool.ntp.org
- name: tell nagios to unshush
nagios: action=unsilence service=host host={{ inventory_hostname_short }}{{ env_suffix }}
delegate_to: noc01.iad2.fedoraproject.org
ignore_errors: true
when: nonagios is not defined or not nonagios
- name: post reboot tasks
hosts: myvms_postreboot
user: root
gather_facts: false
serial: 1
tasks:
- name: Wait for host to come back (ipa or rabbitmq)
local_action: wait_for port=22 delay=30 timeout=300 state=started host={{ inventory_hostname }}
when: inventory_hostname.startswith(('ipa', 'rabbitmq'))
- name: restart gssproxy if we rebooted a ipa server
service: name=gssproxy state=restarted
when: inventory_hostname.startswith('ipa')
- name: restart rabbitmq if we rebooted a rabbit server
service: name=rabbitmq-server state=restarted
when: inventory_hostname.startswith('rabbitmq')
# Call out to that dns playbook. Put proxies back in now that they're back
- name: update proxy dns if needed
import_playbook: update-proxy-dns.yml
vars:
status: enable
proxies: myvms_new:&proxies
when: nodns is not defined or not nodns
# - name: get info on guests (postreboot)
# virt: command=info
# register: vminfo_post