Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

New ec2_group_facts module to be able to get facts from EC2 security groups #2591

Merged
merged 7 commits into from
Nov 11, 2016

Conversation

Sodki
Copy link
Contributor

@Sodki Sodki commented Jul 20, 2016

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

ec2_group_facts

ANSIBLE VERSION
ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
SUMMARY

Ansible doesn't have a way to obtain information from EC2 security groups, which is very useful. This implementation is heavily inspired on ec2_snapshot_facts.

The only option available is filter. I didn't implement boto3's GroupNames and GroupIds because they're a source of major confusion (default vs non-default VPC) and filter has that functionality anyway, so I thought it was better to keep it simple.

I know there are other PRs for the same module, but they were either broken or had a larger scope than what was intended. I'll list them here for reference:
#1939
#1908
#1352

@dfjaimes
Copy link

#1939 was closed a while back, so its irrelevant here
#1352 is certainly out of scope
#1908 isn't broken, nor is it out of scope - its just waiting on review again

I wrote it back in the boto2 days then retrofitted it to boto3 for the PR, so if the GroupNames and GroupIds aren't needed, I could simply pull that logic out - but, that hasn't been brought up in past reviews so I've never given it any thought.

@gregdek
Copy link
Contributor

gregdek commented Jul 20, 2016

Thanks @Sodki for this new module. When this module receives 'shipit' comments from two community members and any 'needs_revision' comments have been resolved, we will mark for inclusion.

[This message brought to you by your friendly Ansibull-bot.]

@Sodki
Copy link
Contributor Author

Sodki commented Jul 21, 2016

@calamityman Cool, at the time of writing this your PR was still in old boto. Instead of spamming your PR with comments, can I just send a bunch of PRs to your own branch, to try and make it better? Then I can close this one down.

@dfjaimes
Copy link

@Sodki of course. I'd be more than happy to work with you on this module if you've got some improvements.

@Sodki
Copy link
Contributor Author

Sodki commented Jul 25, 2016

@calamityman Unfortunately it doesn't seem to be possible to send PRs to your branch, since it's a fork. I'll comment straight to your PR.

@Sodki
Copy link
Contributor Author

Sodki commented Aug 15, 2016

#2619 is another PR related to this one.

@Sodki
Copy link
Contributor Author

Sodki commented Sep 14, 2016

ready_for_review

@aairey
Copy link

aairey commented Oct 24, 2016

Any update on this? Seems stalled.

@Sodki
Copy link
Contributor Author

Sodki commented Oct 24, 2016

@aairey Well, the module is finished and works, but no one has reviewed it yet. I don't know if there's something else that I need to do to speed things up.

Copy link
Contributor

@ryansb ryansb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks great, and works in my environment. Just a couple changes before we can get this merged in.


# Gather facts about any security group with a tag key Name and value Example
- ec2_group_facts:
filters:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this example can you add a comment to the effect of "The quotes around tag:Name are important because of the colon in the value" because not everyone realizes the significance there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment was spot on, so I just used it verbatim.

# Gather facts about a security group by id
- ec2_group_facts:
filters:
group-id: sg-12345678
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be really nice to be able to do underscore-separated tags as well, since that's typically how folks expect to do params. Do you think that'd be a useful addition?

diff --git a/cloud/amazon/ec2_group_facts.py b/cloud/amazon/ec2_group_facts.py
index 777fc59..bc9a75d 100644
--- a/cloud/amazon/ec2_group_facts.py
+++ b/cloud/amazon/ec2_group_facts.py
@@ -121,9 +121,11 @@ def main():
     else:
         module.fail_json(msg="region must be specified")

+    sanitized_filters = {k.replace('_', '-'): v for k, v in module.params.get("filters", {}).items() if 'tag:' not in k}
+    sanitized_filters.update({k: v for k, v in module.params.get("filters", {}).items() if 'tag:' in k})
     try:
         security_groups = connection.describe_security_groups(
-            Filters=ansible_dict_to_boto3_filter_list(module.params.get("filters"))
+            Filters=ansible_dict_to_boto3_filter_list(sanitized_filters)
         )
     except ClientError as e:
         module.fail_json(msg=e.message)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea. I've implemented it a little bit different from you, but I think it's easier to read. I've also changed the documentation accordingly.

Filters=ansible_dict_to_boto3_filter_list(module.params.get("filters"))
)
except ClientError as e:
module.fail_json(msg=e.message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here can you add the exception to the failure, something like:

import traceback # do this with the other imports, not in the exception
module.fail_json(msg=e.message, exception=traceback.format_exc(e))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done. Let me know if you disagree with the location of the import. I really don't have others like that one and I wanted to keep the boto ones separate.

short_description: Gather facts about ec2 security groups in AWS.
description:
- Gather facts about ec2 security groups in AWS.
version_added: "2.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update this to 2.3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been updated to 2.3.

@Sodki
Copy link
Contributor Author

Sodki commented Nov 11, 2016

Thank you @ryansb for your review. I've implemented all your change requests as separate commits because I think it's easier to understand the flow. If you're happy I can then squash them all before merging.

# Gather facts about a security group with multiple filters, also mixing the use of underscores as filter keys
- ec2_group_facts:
filters:
group_id: sg-12345678
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


sanitized_filters = module.params.get("filters")
for key in sanitized_filters:
sanitized_filters[key.replace("_", "-")] = sanitized_filters.pop(key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one issue with this: if someone uses a filter such as "tag:my_tag_name": value this code will replace it and make the filter tag:my-tag-name. In the example I gave it skipped replacing _ for any tag: filters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I completely forgot about that edge case. It's fixed now.

@ryansb
Copy link
Contributor

ryansb commented Nov 11, 2016

@Sodki really close, just that last comment on filter underscore-replacement. No worries about squashing this PR, I can do so on merge. Thanks for doing separate commits, makes reviewing much easier. 😄

@Sodki
Copy link
Contributor Author

Sodki commented Nov 11, 2016

Alright, I fixed the last issue. Thanks again for taking the time to review this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants