---
- name: "Load Balancer | {{ system_role }}"
  ec2_elb_lb:
    state: present
    region: "{{ region }}"
    name: "{{ project }}-{{ system_role }}"
    security_group_ids: "{% for security_group in load_balancer[system_role].security_groups %}{{ security_group_id[security_group] }}{% if not loop.last %},{% endif %}{% endfor %}"
    listeners: "{{ load_balancer[system_role].listeners }}"
    scheme: "{{ load_balancer[system_role].scheme }}"
    subnets: "{{ vpc.subnets | selectattr('resource_tags.Role', 'equalto', system_role) | map(attribute='id') | list }}"
    cross_az_load_balancing: yes
  register: balancer

- name: "Capture Load Balancer Name | {{ system_role }}"
  set_fact:
    load_balancer_name: "{{ load_balancer_name.update({system_role: balancer.elb.name}) }}{{ load_balancer_name }}"

- name: "Route53 Record | {{ system_role }}.{{ project }}-{{ environment_tier }}.{{ domain }}"
  route53:
    command: create
    overwrite: yes
    zone: "{{ domain }}"
    record: "{{ system_role }}.{{ project }}-{{ environment_tier }}.{{ domain }}"
    type: CNAME
    value: "{{ balancer.elb.dns_name }}"
    ttl: 300
  when: dns_points_to_load_balancer
  tags:
    - dns

---
- name: Check ELB name length
  debug:
    msg: "ELB name '{{ project }}-{{ system_role }}' is greater then 32 characters"
  failed_when: "{{ ((project + '-' + system_role) | length) > 32 }}"

- name: "Load Balancer | {{ system_role }}"
  ec2_elb_lb:
    state: present
    region: "{{ region }}"
    name: "{{ project }}-{{ system_role }}"
    security_group_ids: "{% for security_group in load_balancer[system_role].security_groups %}{{ security_group_id[security_group] }}{% if not loop.last %},{% endif %}{% endfor %}"
    listeners: "{{ load_balancer[system_role].listeners }}"
    scheme: "{{ load_balancer[system_role].scheme }}"
    subnets: "{{ vpc.subnets | selectattr('resource_tags.Role', 'equalto', system_role) | map(attribute='id') | list }}"
    cross_az_load_balancing: yes
  register: balancer

- name: "Capture Load Balancer Name | {{ system_role }}"
  set_fact:
    load_balancer_name: "{{ load_balancer_name.update({system_role: balancer.elb.name}) }}{{ load_balancer_name }}"

- name: "Route53 Record | {{ system_role }}.{{ project }}-{{ environment_tier }}.{{ domain }}"
  route53:
    command: create
    overwrite: yes
    zone: "{{ domain }}"
    record: "{{ system_role }}.{{ project }}-{{ environment_tier }}.{{ domain }}"
    type: CNAME
    value: "{{ balancer.elb.dns_name }}"
    ttl: 300
  when: dns_points_to_load_balancer
  tags:
    - dns

Test elb name for > 32 char and fail if that is the case
Test elb name for > 32 char and fail if that is the case
