Creating multiple rules on an AWS load balancer listener

Hi All,

I have an application load balancer in AWS with an HTTP listener configured (another tool provisioned it, not ansible). Anyway my task is to add rules to its HTTP listener using Ansible by looping through a vars file.

I first use Ansible’s various AWS get_fact operations to discover the load balancer, it’s ARNs, subnetIDs etc. Then I define the load balancer using Ansible’s elb_application_lb module and use the facts I’ve previously retrieved to set the properties for it’s name, security groups etc. All of this works beautifully well.

Then under the HTTP listener properties, I define one rule using the “with_items” feature. My hope would be that it’d loop through my list, creating the rules I want (and it does kind of do this…), however instead of appending the rules to the listener as it goes, it instead deletes the existing rule then creates the next one. So when the playbook completes I’m left with the ALB with just one listener rule defined (and it will be whichever entry is last in my vars file). The code is below:

  • name: Add HTTP listener rules
    elb_application_lb:
    state: present
    name: “{{ albinfo.load_balancer_name }}”
    subnets:
  • “{{ albinfo.availability_zones[0].subnet_id }}”
  • “{{ albinfo.availability_zones[1].subnet_id }}”
  • “{{ albinfo.availability_zones[2].subnet_id }}”
    security_groups:
  • “{{ albinfo.security_groups[0] }}”
    listeners:
  • Protocol: HTTP
    Port: 80
    DefaultActions:
  • Type: forward
    TargetGroupName: test
    Rules:
  • Conditions:
  • Field: host-header
    Values: “{{ item.url }}”
    ListenerArn: “{{ albinfo.listeners[0].listener_arn }}”
    Priority: “{{ item.priority }}”
    Actions:
  • TargetGroupName: “{{ item.name }}”
    Type: forward
    purge_listeners: no
    with_items: “{{ regions }}”

And my “regions” var file looks like this:

regions:

I’m sure that this is a logic error on my part and not a bug, so could anybody explain where I’m going wrong?

Hi There,
Seems like the code is executing the entire block twice, ie the ELB creation is getting executed twice.
Here is the reference document: http://docs.ansible.com/ansible/latest/playbooks_loops.html

So if we pretend I was updating two ALBs with their own rulesets, I’d be trying to loop within a loop? I’ll give nested loops a try in that case. Thanks