Instantly share code, notes, and snippets.

What would you like to do?
Ansible filter plugin to create rules fo ec2_group
def make_rules(hosts, ports, proto):
return [{"proto": proto,
"from_port": port,
"to_port": port,
"cidr_ip": host} for host in hosts for port in map(int, ports.split(","))]
class FilterModule(object):
def filters(self):
return {'make_rules': make_rules}
rules: "{{ random_hosts | make_rules('22,8080', 'tcp') }}"

This comment has been minimized.

jale2014 commented Jan 7, 2016

Works after leaving out "ports" input variable and the "double" loop "for port in map(int, ports.split(","))" and using "from_port" and "to_port" variables instead.


This comment has been minimized.

aioue commented May 3, 2016

@jale2014 do you have the working version? @viesti?


This comment has been minimized.

mabushey commented Jun 8, 2017

def make_rules(hosts, from_port, to_port, proto):
    return [{"proto": proto,
             "from_port": from_port,
             "to_port": to_port,
             "cidr_ip": host} for host in hosts]

call with rules: "{{ random_hosts | make_rules(22, 8080, 'tcp') }}"


This comment has been minimized.

sysnet4admin commented Jan 11, 2018

Hi viesti,

May I use this code for education purpose?
I already posted some article to use this code...actually.
Because one of user member mention this code to verify in Korea ansible user community..
Thus, I aim to look into this code.
If you have any unconcern it, please let me know.

Here is article link:

Thank you in advance,
Hoon Jo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment