forked from redhat-cop/agnosticd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle_osp.yml
92 lines (82 loc) · 3.26 KB
/
lifecycle_osp.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
- environment:
OS_AUTH_URL: "{{ osp_auth_url }}"
OS_USERNAME: "{{ osp_auth_username }}"
OS_PASSWORD: "{{ osp_auth_password }}"
OS_PROJECT_NAME: "{{ osp_project_name }}"
OS_PROJECT_DOMAIN_ID: "{{ osp_auth_project_domain }}"
OS_USER_DOMAIN_NAME: "{{ osp_auth_user_domain }}"
block:
- when: ACTION == 'stop'
block:
- name: Gather instance facts
os_server_facts:
filters:
metadata:
guid: "{{ guid }}"
env_type: "{{ env_type }}"
vm_state: active
register: r_osp_facts
- when: r_osp_facts.ansible_facts.openstack_servers | length > 0
block:
- set_fact:
all_instances: >-
{{ r_osp_facts.ansible_facts.openstack_servers
| json_query('[*].id') }}
- name: Stop all servers
command: openstack server shelve {{ all_instances | join(' ') }}
- when: ACTION == 'start'
block:
- name: Gather instance facts
os_server_facts:
filters:
metadata:
guid: "{{ guid }}"
env_type: "{{ env_type }}"
register: r_osp_facts
- when: r_osp_facts.ansible_facts.openstack_servers | length > 0
block:
- set_fact:
all_instances: "{{ r_osp_facts.ansible_facts.openstack_servers
| json_query('[*].{id:id, vm_state:vm_state}') }}"
- name: Start servers
command: openstack server start {{ _stopped_instances | join(" ") }}
vars:
_stopped_instances: >-
{{ all_instances | json_query("[?vm_state == 'stopped'].id") }}
when: _stopped_instances | length > 0
- name: Unshelve servers
command: openstack server unshelve {{ _shelved_instances | join(" ") }}
vars:
_shelved_instances: >-
{{ all_instances | json_query("[?vm_state == 'shelved_offloaded'].id") }}
when: _shelved_instances | length > 0
- when: ACTION == 'status'
block:
- name: Get OSP facts using (guid, env_type) metadata
os_server_facts:
filters:
metadata:
guid: "{{ guid }}"
env_type: "{{ env_type }}"
register: r_instances
- debug:
var: r_instances
verbosity: 3
- name: Report status in user info
agnosticd_user_info:
msg: |-
{{ "%-30s %s" | format('Instance', 'State') }}
-------------------------------------------
{% for instance in r_instances.ansible_facts.openstack_servers %}
{{ "%-30s %s" | format(instance.name, instance.vm_state) }}
{% endfor %}
- name: Print status information to a file
copy:
dest: "{{ output_dir }}/status.txt"
content: |-
{{ "%-30s %s" | format('Instance', 'State') }}
-------------------------------------------
{% for instance in r_instances.ansible_facts.openstack_servers %}
{{ "%-30s %s" | format(instance.name, instance.vm_state) }}
{% endfor %}