- name: Deploy rabbitmq
  hosts: all
  become: True
  # The rabbitmq role depends on
  # ansible_default_ipv4 so
  # gather_facts must be set to True
  gather_facts: True
  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
    CLUSTER_NAME: 'rabbitmq'
  serial: "{{ serial_count }}"
  pre_tasks:
    - action: ec2_metadata_facts
      when: elb_pre_post
    - debug:
        var: ansible_ec2_instance_id
      when: elb_pre_post
    - name: Instance De-register
      local_action: ec2_elb
      args:
        instance_id: "{{ ansible_ec2_instance_id }}"
        region: us-east-1
        state: absent
        wait_timeout: 60
      become: False
      when: elb_pre_post
  roles:
    - role: aws
      when: COMMON_ENABLE_AWS_ROLE
    - role: automated
      AUTOMATED_USERS: "{{ RABBIT_AUTOMATED_USERS | default({}) }}"
      tags: 
        - automated_role
    - rabbitmq
    - role: splunkforwarder
      when: COMMON_ENABLE_SPLUNKFORWARDER
    - role: newrelic_infrastructure
      when: COMMON_ENABLE_NEWRELIC_INFRASTRUCTURE
  post_tasks:
    - debug:
        var: ansible_ec2_instance_id
      when: elb_pre_post
    - name: Register instance in the elb
      local_action: ec2_elb
      args:
        instance_id: "{{ ansible_ec2_instance_id }}"
        ec2_elbs: "{{ ec2_elbs }}"
        region: us-east-1
        state: present
        wait_timeout: 60
      become: False
      when: elb_pre_post
