openstack-ansible-ops/multi-node-aio/playbooks/vm-status.yml

109 lines
3.0 KiB
YAML

---
# Copyright 2018, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in witing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Create vm_servers group
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Create vm_servers group
add_host:
name: "{{ item }}"
groups: vm_servers
when:
- (hostvars[item]['server_vm'] | default(false)) | bool
with_items: "{{ groups['pxe_servers'] }}"
- name: VM Status
hosts: vm_servers
gather_facts: false
tasks:
- name: VM Connectivity Check
block:
- name: Wait for VM
wait_for_connection:
connect_timeout: 10
sleep: 20
timeout: "{{ vm_ssh_timeout }}"
rescue:
- name: Gather VM info (rescue)
virt:
command: status
name: "{{ inventory_hostname }}"
connection: local
register: vm_info
- name: Stop VM (rescue)
virt:
command: destroy
name: "{{ inventory_hostname }}"
connection: local
when: vm_info.status == 'running'
- name: Start VM (rescue)
virt:
command: start
name: "{{ inventory_hostname }}"
connection: local
- name: Wait for VM (rescue)
wait_for_connection:
connect_timeout: 10
sleep: 20
timeout: "{{ vm_ssh_timeout }}"
register: vm_rescue
ignore_errors: true
- name: Gather VM info 2nd pass (rescue)
virt:
command: status
name: "{{ inventory_hostname }}"
connection: local
register: vm_info_2
- name: Fail if VM still offline (rescue)
fail:
msg: "{{ inventory_hostname }} is not responding and cannot be rescued"
when:
- vm_info_2.status != 'running'
- vm_rescue.failed == 'true'
- name: Refresh the inventory
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Refresh the inventory
meta: refresh_inventory
- name: Create vm_servers group
add_host:
name: "{{ item }}"
groups: vm_servers
when:
- (hostvars[item]['server_vm'] | default(false)) | bool
with_items: "{{ groups['pxe_servers'] }}"
- name: Container Status
hosts: all_containers
gather_facts: false
tasks:
- name: Wait for container connectivity
wait_for_connection:
connect_timeout: 10
delay: 3
sleep: 20
timeout: "{{ vm_ssh_timeout }}"