Creating multiple rules on an AWS load balancer listener

608 views
Skip to first unread message

geo...@divirted.com

unread,
Feb 4, 2018, 9:59:15 AM2/4/18
to Ansible Project
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:
  - name: manchester
    priority: 1
    url:

  - name: surrey
    priority: 2
    url:


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?

eshan TANDON

unread,
Feb 4, 2018, 10:44:34 PM2/4/18
to Ansible Project
Hi There,
Seems like the code is executing the entire block twice, ie the ELB creation is getting executed twice.

--------------------------------------------------------------------------
- name: add several users
  user:
    name: "{{ item }}"
    state: present
    groups: "wheel"
  with_items:
     - testuser1
     - testuser2
is equivalent to:
- name: add user testuser1
  user:
    name: "testuser1"
    state: present
    groups: "wheel"
- name: add user testuser2
  user:
    name: "testuser2"
    state: present
    groups: "wheel"




geo...@divirted.com

unread,
Feb 5, 2018, 3:53:32 AM2/5/18
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages