# ansible-playbook -i ec2.py commoncluster.yml --limit tag_Name_stage-edx-commoncluster -e@/path/to/vars/env-deployment.yml -T 30 --list-hosts

- hosts: all
  sudo: True
  serial: 1
  vars:
    # By default take instances in and out of the elb(s) they
    # are attached to
    # To skip elb operations use "-e elb_pre_post=fase"
    elb_pre_post: true
    # Number of instances to operate on at a time
    serial_count: 1
  serial: "{{ serial_count }}"
  pre_tasks:
    - action: ec2_facts
      when: elb_pre_post
    - debug: var="{{ ansible_ec2_instance_id }}"
      when: elb_pre_post
    - name: Instance De-register
      local_action: ec2_elb_local_1.6.2
      args:
        instance_id: "{{ ansible_ec2_instance_id }}"
        region: us-east-1
        state: absent
      sudo: False
      when: elb_pre_post
  roles:
    - aws
    - role: nginx
      nginx_sites:
      - xqueue
    - role: xqueue
    - role: datadog
      when: COMMON_ENABLE_DATADOG
    - role: splunkforwarder
      when: COMMON_ENABLE_SPLUNKFORWARDER
    - role: newrelic
      when: COMMON_ENABLE_NEWRELIC
    - oraclejdk
    - elasticsearch
    - rabbitmq
    - datadog
    - splunkforwarder
  post_tasks:
    - debug: var="{{ ansible_ec2_instance_id }}"
      when: elb_pre_post
    - name: Register instance in the elb
      local_action: ec2_elb_local_1.6.2
      args:
        instance_id: "{{ ansible_ec2_instance_id }}"
        ec2_elbs: "{{ item }}"
        region: us-east-1
        state: present
      with_items: ec2_elbs
      sudo: False
      when: elb_pre_post
#
# In order to reconfigure the host resolution we are issuing a
# reboot.
# TODO: We should probably poll to ensure the host comes back before moving
# to the next host so that we don't reboot all of the servers simultaneously
- hosts: all
  sudo: True
  serial: 1
  vars:
    reboot: False
  tasks:
    - name: reboot
      command: /sbin/shutdown -r now "Reboot is triggered by Ansible"
      when: reboot
      tags: reboot