---
|
# This file is the default playbook for common actions.
|
# You should implement those actions in your config if you
|
# need a specific process.
|
|
- import_playbook: setup_runtime.yml
|
|
- name: Run stop/start/status/... actions
|
hosts: localhost
|
connection: local
|
gather_facts: False
|
become: no
|
tasks:
|
- fail:
|
msg: "project_tag is not defined"
|
when: project_tag is not defined or project_tag == ''
|
|
- fail:
|
msg: "ACTION is not defined"
|
when: ACTION is not defined
|
|
- when: >-
|
guid is not defined
|
or guid == ''
|
or guid == '*'
|
fail:
|
msg: variable 'guid' must be defined and not empty
|
|
- when:
|
- cloud_provider == 'ec2'
|
- guid is defined
|
- guid != ''
|
- guid != '*'
|
environment:
|
AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
|
AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
|
AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
|
block:
|
- when: ACTION == 'stop'
|
name: Stop instances by (guid,env_type) tags
|
ec2_instance:
|
state: stopped
|
wait: no
|
filters:
|
"tag:guid": "{{ guid }}"
|
# TODO: uncomment this after a few weeks
|
#"tag:env_type": "{{ env_type }}"
|
|
- when: ACTION == 'start'
|
name: Start instances by (guid, env_type) tags
|
ec2_instance:
|
state: started
|
wait: no
|
filters:
|
"tag:guid": "{{ guid }}"
|
# TODO: uncomment this after a few weeks
|
#"tag:env_type": "{{ env_type }}"
|
|
- when: ACTION == 'status'
|
block:
|
- name: Get EC2 facts using (guid, env_type) tag
|
ec2_instance_facts:
|
filters:
|
"tag:guid": "{{ guid }}"
|
# TODO: uncomment this after a few weeks
|
#"tag:env_type": "{{ env_type }}"
|
register: r_instances
|
|
- name: Print status information to a file
|
copy:
|
dest: "{{ output_dir }}/status.txt"
|
content: |-
|
{{ "%-60s" | format('Instance') }} State Type
|
----------------------------------------------------------------
|
{% for instance in r_instances.instances %}
|
{{ "%-60s" | format(instance.tags.Name) }} {{ "%-10s" | format(instance.state.name) }} {{ instance.instance_type }}
|
{% endfor %}
|
- when: cloud_provider == 'osp'
|
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 stop {{ all_instances | join(' ') }}
|
|
- when: ACTION == 'start'
|
block:
|
- name: Gather instance facts
|
os_server_facts:
|
filters:
|
metadata:
|
guid: "{{ guid }}"
|
env_type: "{{ env_type }}"
|
vm_state: stopped
|
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: Start all servers
|
command: openstack server start {{ all_instances | join(' ') }}
|
|
- 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
|
|
- name: Print status information to a file
|
copy:
|
dest: "{{ output_dir }}/status.txt"
|
content: |-
|
{{ "%-30s" | format('Instance') }} State
|
-------------------------------------------
|
{% for instance in r_instances.ansible_facts.openstack_servers %}
|
{{ "%-30s" | format(instance.name) }} {{ "%-10s" | format(instance.vm_state) }}
|
{% endfor %}
|