Script/How to export from ec2_group_facts and import into ec2_group? AWS

Dear Citizens of Ansible Project,

I may have a need to export large numbers of security groups from VPCs and import them into new VPCs; basically I want to clone all the security groups from one VPC, and import them to another.

Naturally, I thought of doing this using Ansible.

I have found scripts that dump ec2_group_facts or similar and migrate them to new regions for example: https://github.com/Suncatcher/aws_sg_migrate

To my mind, it would be preferable to have a copy of the security groups in ec2_group format, so that I can use Ansible to make the changes. Going forward, people who edit the Ansible ec2_group rules should commit changes to Git first, so we can track who makes changes to security rules for auditing purposes. It seems preferable to use Ansible as tool of choice, but maybe there is a better way? My initial impression is that it makes sense to maintain Ansible playbooks using ec2_group to maintain Security Groups.

There are a fairly large number of Security Groups that need to be maintained, so if there’s a script out there that exports from ec2_group_facts and imports into ec2_group it would save a lot of time

Ec2_group_facts output looks like this:

Hullo Simon

Did you ever figure this out?

It’s a mystery to my why the writer of the ec2_group_facts module would generate output that could not be used as input for ec2_group…

Regards, K.

Hi Karl,

It does seem a bit odd to me. I ended up using Vim search and replace functions to make the changes, and did some of the work manually. On a related note, I discovered two things that may be useful to people faced with a similar task: you can insert multiple ports in a rule, as a list, one after another instead of having a new rule for each port; when you export the rules, it exports each port as it’s own rule, which makes for a lot of unnecessary work when editing. Also, there is a clone option in AWS GUI which I didn’t realize; this might also come in handy. In the end what I noticed though is that once you get familiar with the Ansible ec2_group syntax, it’s often faster to make the change to the script then through the GUI specifically because it is possible to insert multiple ports in one rule, it’s like you can collapse large numbers of rules into one rule with a list of ports; if you make changes to security groups through the GUI, it seems to me that you have to make a new rule for each port.

Onwards,

Yes - there’s a relatively new syntax in Ansible that allows source groups of ports, ranges and other groups.

I need to do my changes dynamically - I can’t take code, edit it manually, then run something, I need to be able to grab what’s there, add a rule and write it back.

Having looked at the two modules, it looks as if modifying ec2_group_facts to generate group-compatible output would be quite hard. It’s a very simple module that essentially returns what the AWS API returns. But it looks as if it would be less difficult to make ec2_group accept that output, and it would be able to discriminate between the two formats quite easily. At the moment it validates one format; just validate that, if it fails validate the other, and if that fails, complain.

I hope someone fixes this. Neither your nor my issues can be uncommon…

Regards, K.
.

You don’t need to include the existing rules in your task; if you set purge_rules to false the current rules will be preserved.

Well - dang.

If that’s all I need, you just saved me a whole heap of time. Thanks!

But I still think that a facts module should provide facts that can be used as input fo create/modify modules :slight_smile:

Regards, K.