Nate Stephany
2019-12-09 8507b89f4cbf66b0d4158687fcf9a39e70516b5f
commit | author | age
aeb735 1 ---
GC 2 # This file is the default playbook for common actions.
3 # You should implement those actions in your config if you
4 # need a specific process.
5
6 - import_playbook: setup_runtime.yml
7
8 - name: Run stop/start/status/... actions
9   hosts: localhost
10   connection: local
11   gather_facts: False
12   become: no
13   tasks:
14     - fail:
15         msg: "project_tag is not defined"
16       when: project_tag is not defined or project_tag == ''
17
18     - fail:
19         msg: "ACTION is not defined"
20       when: ACTION is not defined
21
d84223 22     - when: >-
GC 23         guid is not defined
24         or guid == ''
25         or guid == '*'
26       fail:
27         msg: variable 'guid' must be defined and not empty
28
7b4d8b 29     - when:
GC 30         - cloud_provider == 'ec2'
31         - guid is defined
32         - guid != ''
33         - guid != '*'
aeb735 34       environment:
GC 35         AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
36         AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
37         AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
38       block:
39         - when: ACTION == 'stop'
e6c7d8 40           name: Stop instances by (guid,env_type) tags
GC 41           ec2_instance:
42             state: stopped
43             wait: no
44             filters:
45               "tag:guid": "{{ guid }}"
7b4d8b 46               # TODO: uncomment this after a few weeks
GC 47               #"tag:env_type": "{{ env_type }}"
e6c7d8 48
GC 49         - when: ACTION == 'start'
50           name: Start instances by (guid, env_type) tags
51           ec2_instance:
52             state: started
53             wait: no
54             filters:
55               "tag:guid": "{{ guid }}"
7b4d8b 56               # TODO: uncomment this after a few weeks
GC 57               #"tag:env_type": "{{ env_type }}"
ae7540 58
GC 59         - when: ACTION == 'status'
60           block:
61             - name: Get EC2 facts using (guid, env_type) tag
62               ec2_instance_facts:
63                 filters:
64                   "tag:guid": "{{ guid }}"
7b4d8b 65                   # TODO: uncomment this after a few weeks
GC 66                   #"tag:env_type": "{{ env_type }}"
ae7540 67               register: r_instances
GC 68
69             - name: Print status information to a file
70               copy:
71                 dest: "{{ output_dir }}/status.txt"
72                 content: |-
acec6d 73                   {{ "%-60s" | format('Instance') }} State      Type
ae7540 74                   ----------------------------------------------------------------
GC 75                   {% for instance in r_instances.instances %}
acec6d 76                   {{ "%-60s" | format(instance.tags.Name) }} {{ "%-10s" | format(instance.state.name) }} {{ instance.instance_type }}
ae7540 77                   {% endfor %}
d84223 78     - when: cloud_provider == 'osp'
GC 79       environment:
80         OS_AUTH_URL: "{{ osp_auth_url }}"
81         OS_USERNAME: "{{ osp_auth_username }}"
82         OS_PASSWORD: "{{ osp_auth_password }}"
83         OS_PROJECT_NAME: "{{ osp_project_name }}"
84         OS_PROJECT_DOMAIN_ID: "{{ osp_auth_project_domain }}"
85         OS_USER_DOMAIN_NAME: "{{ osp_auth_user_domain }}"
86       block:
87         - when: ACTION == 'stop'
88           block:
89             - name: Gather instance facts
90               os_server_info:
91                 filters:
92                   metadata:
93                     guid: "{{ guid }}"
94                     env_type: "{{ env_type }}"
95                   vm_state: active
96               register: r_osp_facts
ae7540 97
8507b8 98             - when: r_osp_facts.ansible_facts.openstack_servers | length > 0
d84223 99               block:
GC 100                 - set_fact:
101                     all_instances: >-
8507b8 102                       {{ r_osp_facts.ansible_facts.openstack_servers
d84223 103                       | json_query('[*].id') }}
GC 104
105                 - name: Stop all servers
106                   command: openstack server stop {{ all_instances | join(' ') }}
107
108         - when: ACTION == 'start'
109           block:
110             - name: Gather instance facts
111               os_server_info:
112                 filters:
113                   metadata:
114                     guid: "{{ guid }}"
115                     env_type: "{{ env_type }}"
116                   vm_state: stopped
117               register: r_osp_facts
118
8507b8 119             - when: r_osp_facts.ansible_facts.openstack_servers | length > 0
d84223 120               block:
GC 121                 - set_fact:
122                     all_instances: >-
8507b8 123                       {{ r_osp_facts.ansible_facts.openstack_servers
d84223 124                       | json_query('[*].id') }}
GC 125
126                 - name: Start all servers
127                   command: openstack server start {{ all_instances | join(' ') }}
128
129         - when: ACTION == 'status'
130           block:
131             - name: Get OSP facts using (guid, env_type) metadata
132               os_server_info:
133                 filters:
134                   metadata:
135                     guid: "{{ guid }}"
136                     env_type: "{{ env_type }}"
137               register: r_instances
138
139             - name: Print status information to a file
140               copy:
141                 dest: "{{ output_dir }}/status.txt"
142                 content: |-
143                   {{ "%-30s" | format('Instance') }} State      Type
144                   ----------------------------------------------------------------
145                   {% for instance in r_instances.openstack_servers %}
146                   {{ "%-30s" | format(instance.name) }} {{ "%-10s" | format(instance.vm_state) }} {{ instance.flavor.original_name }}
147                   {% endfor %}