From 610223fbf4047f9288155406dad3729cb0dcc7de Mon Sep 17 00:00:00 2001 From: Alex Lo Date: Wed, 13 May 2015 23:54:52 -0400 Subject: [PATCH 1/3] explain source of EC2 inventory error https://github.com/ansible/ansible/issues/10840 before RDS: `ERROR: Inventory script (ec2.py) had an execution error: Forbidden` EC2: `ERROR: Inventory script (ec2.py) had an execution error: Error connecting to AWS backend. You are not authorized to perform this operation.` after RDS: `ERROR: Inventory script (ec2.py) had an execution error: ERROR: "Forbidden", while: getting RDS instances` EC2: `ERROR: Inventory script (ec2.py) had an execution error: ERROR: "Error connecting to AWS backend. You are not authorized to perform this operation.", while: getting EC2 instances` --- contrib/inventory/ec2.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index f2d9b51c903..e17e41cc689 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -406,7 +406,9 @@ class Ec2Inventory(object): else: backend = 'Eucalyptus' if self.eucalyptus else 'AWS' error = "Error connecting to %s backend.\n%s" % (backend, e.message) - self.fail_with_error(error) + self.fail_with_error( + 'ERROR: "{error}", while: {err_operation}'.format( + error=error, err_operation='getting EC2 instances')) def get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular @@ -425,7 +427,9 @@ class Ec2Inventory(object): error = self.get_auth_error_message() if not e.reason == "Forbidden": error = "Looks like AWS RDS is down:\n%s" % e.message - self.fail_with_error(error) + self.fail_with_error( + 'ERROR: "{error}", while: {err_operation}'.format( + error=error, err_operation='getting RDS instances')) def get_elasticache_clusters_by_region(self, region): ''' Makes an AWS API call to the list of ElastiCache clusters (with From 17b94cf139ca1882e8c827010a3c4aa4fa624ba6 Mon Sep 17 00:00:00 2001 From: Alex Lo Date: Mon, 13 Jul 2015 23:46:33 -0400 Subject: [PATCH 2/3] generalize error context reporting, add elasticache explanations --- contrib/inventory/ec2.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index e17e41cc689..f0b01ef194c 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -406,9 +406,7 @@ class Ec2Inventory(object): else: backend = 'Eucalyptus' if self.eucalyptus else 'AWS' error = "Error connecting to %s backend.\n%s" % (backend, e.message) - self.fail_with_error( - 'ERROR: "{error}", while: {err_operation}'.format( - error=error, err_operation='getting EC2 instances')) + self.fail_with_error(error, 'getting EC2 instances') def get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular @@ -427,9 +425,7 @@ class Ec2Inventory(object): error = self.get_auth_error_message() if not e.reason == "Forbidden": error = "Looks like AWS RDS is down:\n%s" % e.message - self.fail_with_error( - 'ERROR: "{error}", while: {err_operation}'.format( - error=error, err_operation='getting RDS instances')) + self.fail_with_error(error, 'getting RDS instances') def get_elasticache_clusters_by_region(self, region): ''' Makes an AWS API call to the list of ElastiCache clusters (with @@ -452,7 +448,7 @@ class Ec2Inventory(object): error = self.get_auth_error_message() if not e.reason == "Forbidden": error = "Looks like AWS ElastiCache is down:\n%s" % e.message - self.fail_with_error(error) + self.fail_with_error(error, 'getting ElastiCache clusters') try: # Boto also doesn't provide wrapper classes to CacheClusters or @@ -462,7 +458,7 @@ class Ec2Inventory(object): except KeyError as e: error = "ElastiCache query to AWS failed (unexpected format)." - self.fail_with_error(error) + self.fail_with_error(error, 'getting ElastiCache clusters') for cluster in clusters: self.add_elasticache_cluster(cluster, region) @@ -486,7 +482,7 @@ class Ec2Inventory(object): error = self.get_auth_error_message() if not e.reason == "Forbidden": error = "Looks like AWS ElastiCache [Replication Groups] is down:\n%s" % e.message - self.fail_with_error(error) + self.fail_with_error(error, 'getting ElastiCache clusters') try: # Boto also doesn't provide wrapper classes to ReplicationGroups @@ -496,7 +492,7 @@ class Ec2Inventory(object): except KeyError as e: error = "ElastiCache [Replication Groups] query to AWS failed (unexpected format)." - self.fail_with_error(error) + self.fail_with_error(error, 'getting ElastiCache clusters') for replication_group in replication_groups: self.add_elasticache_replication_group(replication_group, region) @@ -518,8 +514,11 @@ class Ec2Inventory(object): return '\n'.join(errors) - def fail_with_error(self, err_msg): + def fail_with_error(self, err_msg, err_operation_context=None): '''log an error to std err for ansible-playbook to consume and exit''' + if err_operation_context: + err_msg = 'ERROR: "{err_msg}", while: {err_operation}'.format( + err_msg=err_msg, err_operation=err_operation_context) sys.stderr.write(err_msg) sys.exit(1) From 7092021d81c41626f51b765ea8fdc42e376ad905 Mon Sep 17 00:00:00 2001 From: Alex Lo Date: Mon, 13 Jul 2015 23:51:23 -0400 Subject: [PATCH 3/3] simplify variable names --- contrib/inventory/ec2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index f0b01ef194c..be25a5b6943 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -514,11 +514,11 @@ class Ec2Inventory(object): return '\n'.join(errors) - def fail_with_error(self, err_msg, err_operation_context=None): + def fail_with_error(self, err_msg, err_operation=None): '''log an error to std err for ansible-playbook to consume and exit''' - if err_operation_context: + if err_operation: err_msg = 'ERROR: "{err_msg}", while: {err_operation}'.format( - err_msg=err_msg, err_operation=err_operation_context) + err_msg=err_msg, err_operation=err_operation) sys.stderr.write(err_msg) sys.exit(1)