cleanup.yml 1.85 KB
Newer Older
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
# This playbook will clean up the work done in the create_ami and launch_instance playbooks consisting of the following work:
#   - delete the key used to bring up the instance
#   - terminate the instance used to create the AMI
#   - delete the ansible-runtime/<run identifer directory>
#
# Required variables for this playbook:
#
#   - instance_id             - the ec2 instance ID use to create the AMI
#   - keypair_id              - the keypair used to launch the instance.
#
# Other variables
#   - ec2_region              - The region used to create the AMI
#   - hipchat_token           - API token to send messages to hipchat
#   - hipchat_room            - ID or name of the room to send the notification
#   - hipchat_url             - URL of the hipchat API  (defaults to v1 of the api)
#
# Example command line to run this playbook:
#    ansible-playbook -vvvv -i "localhost," -c local \
#       -e @overrides.yml \
#       -e @/tmp/ansible-runtime/d057a5d9-4fc5-4a21-9646-4c135be0b7c8/launch_info.yml \
#       cleanup.yml
#

- hosts: all
  vars:
    ec2_region: us-east-1
    ec2_timeout: 300
    artifact_path: /tmp/ansible-runtime
    hipchat_url: https://api.hipchat.com/v2/
  gather_facts: False
  connection: local
  tasks:

  - name: Delete keypair
    ec2_key:
      state: absent
      region: "{{ ec2_region }}"
      name: "{{ keypair_id }}"

  - name: Terminate Instance
    ec2:
      region: "{{ ec2_region }}"
      state: absent
      instance_ids: "{{ instance_id }}"

  - name: cleanup local file system
    file:
48
      path: "{{ artifact_path }}"
49 50 51
      state: absent

  - name: Send Hipchat notification cleanup has finished
52
    hipchat_2_0_0_1:
53 54 55 56
      api: "{{ hipchat_url }}"
      token: "{{ hipchat_token }}"
      room: "{{ hipchat_room }}"
      msg: "Cleanup for run id: {{ keypair_id }} complete."
57
    ignore_errors: yes
58 59
    when: hipchat_token is defined