# Creating multiple rules on an AWS load balancer listener

**URL:** <https://forum.ansible.com/t/creating-multiple-rules-on-an-aws-load-balancer-listener/26656>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [February 4, 2018, 2:59pm UTC](https://forum.ansible.com/t/creating-multiple-rules-on-an-aws-load-balancer-listener/26656 "2018-02-04T14:59:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![george](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@george](https://forum.ansible.com/u/george)\
**Post date:** [February 4, 2018, 2:59pm UTC](https://forum.ansible.com/t/creating-multiple-rules-on-an-aws-load-balancer-listener/26656/1 "2018-02-04T14:59:15Z")

</div>

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:

- [manchester.example.com](http://manchester.example.com)

- name: surrey  
priority: 2  
url:

- [surrey.example.com](http://surrey.example.com)

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?

---

<div class="post-metadata">

**Author:** ![eshan\_TANDON](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@eshan\_TANDON](https://forum.ansible.com/u/eshan_TANDON)\
**Post date:** [February 5, 2018, 3:44am UTC](https://forum.ansible.com/t/creating-multiple-rules-on-an-aws-load-balancer-listener/26656/2 "2018-02-05T03:44:34Z")

</div>

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](http://docs.ansible.com/ansible/latest/playbooks_loops.html)

---

<div class="post-metadata">

**Author:** ![george](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@george](https://forum.ansible.com/u/george)\
**Post date:** [February 5, 2018, 8:53am UTC](https://forum.ansible.com/t/creating-multiple-rules-on-an-aws-load-balancer-listener/26656/3 "2018-02-05T08:53:32Z")

</div>

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
