<commit_before>---
- name: "Place Spot Request for EC2 Instance | {{ role }}"
  ec2:
    region: "{{ region }}"
    zone: "{{ region }}{{ preferred_availability_zone }}"

    key_name: "{{ bootstrap_key }}"

    group_id: "{{ security_group_id['managed'] }},{{ security_group_id[role] }}"

    instance_tags:
      Project:     "{{ project }}"
      Environment: "{{ environment_tier }}"
      Role:        "{{ role }}"
      Name:        "{{ role }}.{{ project }}-{{ environment_tier }}"

    image: "{{ base_image.ami }}"
    instance_type: "{{ instance_type }}"
    exact_count: 1
    count_tag:
      Project:     "{{ project }}"
      Environment: "{{ environment_tier }}"
      Role:        "{{ role }}"
      Name:        "{{ role }}.{{ project }}-{{ environment_tier }}"

    volumes:
      - device_name: /dev/sda1
        device_type: gp2
        delete_on_termination: yes
        volume_size: "{{ root_volume_size }}"

    spot_price: "{% if environment_tier not in ['production'] %}{{ instance_bid }}{% else %}0{% endif %}"
    wait: yes

    vpc_subnet_id: "{{ vpc.subnets | selectattr('resource_tags.Role', 'equalto', role) | selectattr('az', 'equalto', region+preferred_availability_zone) | join(',', attribute='id') }}"
    assign_public_ip: "{% if environment_tier in ['next', 'development', 'staging', 'production'] %}yes{% else %}no{% endif %}"
  register: instance

- name: "Capture Instance ID | {{ role }}"
  set_fact:
    instances: "{{ instances.update({role: instance.tagged_instances[0].id}) }}{{ instances }}"

- name: "Route53 Record | {{ role }}.{{ project }}-{{ environment_tier }}.teluswebteam.com"
  route53:
    command: create
    overwrite: yes
    zone: teluswebteam.com
    record: "{{ role }}{% if dns_points_to_load_balancer and role in load_balancer %}-direct{% endif %}.{{ project }}-{{ environment_tier }}.teluswebteam.com"
    type: CNAME
    value: "{{ item.public_dns_name }}"
    ttl: 300
  with_items: instance.tagged_instances

- name: Assign Instance to Load Balancer
  ec2_elb:
    state: present
    region: "{{ region }}"
    ec2_elbs: "{{ project }}-{{ role }}"
    instance_id: "{{ instances[role] }}"
  when: load_balance_prototypes and role in load_balancer

# - add_host: name={{ item.public_dns_name }} groups=common,{{ role }}
#   with_items: instance.tagged_instances<commit_msg>Insert instances into ELB without waiting for health checks.<commit_after>---
- name: "Place Spot Request for EC2 Instance | {{ role }}"
  ec2:
    region: "{{ region }}"
    zone: "{{ region }}{{ preferred_availability_zone }}"

    key_name: "{{ bootstrap_key }}"

    group_id: "{{ security_group_id['managed'] }},{{ security_group_id[role] }}"

    instance_tags:
      Project:     "{{ project }}"
      Environment: "{{ environment_tier }}"
      Role:        "{{ role }}"
      Name:        "{{ role }}.{{ project }}-{{ environment_tier }}"

    image: "{{ base_image.ami }}"
    instance_type: "{{ instance_type }}"
    exact_count: 1
    count_tag:
      Project:     "{{ project }}"
      Environment: "{{ environment_tier }}"
      Role:        "{{ role }}"
      Name:        "{{ role }}.{{ project }}-{{ environment_tier }}"

    volumes:
      - device_name: /dev/sda1
        device_type: gp2
        delete_on_termination: yes
        volume_size: "{{ root_volume_size }}"

    spot_price: "{% if environment_tier not in ['production'] %}{{ instance_bid }}{% else %}0{% endif %}"
    wait: yes

    vpc_subnet_id: "{{ vpc.subnets | selectattr('resource_tags.Role', 'equalto', role) | selectattr('az', 'equalto', region+preferred_availability_zone) | join(',', attribute='id') }}"
    assign_public_ip: "{% if environment_tier in ['next', 'development', 'staging', 'production'] %}yes{% else %}no{% endif %}"
  register: instance

- name: "Capture Instance ID | {{ role }}"
  set_fact:
    instances: "{{ instances.update({role: instance.tagged_instances[0].id}) }}{{ instances }}"

- name: "Route53 Record | {{ role }}.{{ project }}-{{ environment_tier }}.teluswebteam.com"
  route53:
    command: create
    overwrite: yes
    zone: teluswebteam.com
    record: "{{ role }}{% if dns_points_to_load_balancer and role in load_balancer %}-direct{% endif %}.{{ project }}-{{ environment_tier }}.teluswebteam.com"
    type: CNAME
    value: "{{ item.public_dns_name }}"
    ttl: 300
  with_items: instance.tagged_instances

- name: Assign Instance to Load Balancer
  ec2_elb:
    state: present
    region: "{{ region }}"
    ec2_elbs: "{{ project }}-{{ role }}"
    instance_id: "{{ instances[role] }}"
    wait: no
  when: dns_points_to_load_balancer and role in load_balancer

# - add_host: name={{ item.public_dns_name }} groups=common,{{ role }}
#   with_items: instance.tagged_instances
{'fraction_non_alphanumeric': 0.17158516020236086, 'fraction_numerical': 0.006745362563237774, 'mean_word_length': 3.409851301115242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749360', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1549, 'n_words': 343}