Hi Jaime,
I had the exact same problem. ec2_group modules recreates the rules every time you use the it, so if you're running it in a loop, it will create the group the rule for the last item only.
A workaround that I implemented is to generate a var.yml with a var defined with the rules out of a template, and then source it dynamically:
---
- name: Create rules
sudo: False
local_action:
module: template src=sg_rules.j2 dest=./roles/postgres-server/vars/rules.yml
- name: Load vars
sudo: False
include_vars: rules.yml
- name: Open ports for DB clients
sudo: False
local_action:
module: ec2_group
aws_access_key: "{{ ofertia_s3_access_key }}"
aws_secret_key: "{{ ofertia_s3_secret_key }}"
name: "{{ aws_sg }}"
description: "{{ aws_sg }} group"
region: "{{ aws_region }}"
rules: "{{ security_rules }}"
Where my j2 template is something like:
---
security_rules:
{% for trusted_host in trusted_hosts %}
-
proto: tcp
from_port: 22
to_port: 22
cidr_ip: {{ trusted_host.ip }}/32
-
proto: icmp
from_port: -1
to_port: -1
cidr_ip: {{ trusted_host.ip }}/32
{% endfor %}
Andreub