diff --git a/lib/ansible/modules/cloud/amazon/ecs_ecr.py b/lib/ansible/modules/cloud/amazon/ecs_ecr.py index 94d84c18cc..06c4fde843 100644 --- a/lib/ansible/modules/cloud/amazon/ecs_ecr.py +++ b/lib/ansible/modules/cloud/amazon/ecs_ecr.py @@ -154,6 +154,9 @@ class EcsEcr: self.ecr = boto3_conn(module, conn_type='client', resource='ecr', region=region, endpoint=ec2_url, **aws_connect_kwargs) + self.sts = boto3_conn(module, conn_type='client', + resource='sts', region=region, + endpoint=ec2_url, **aws_connect_kwargs) self.check_mode = module.check_mode self.changed = False self.skipped = False @@ -183,10 +186,14 @@ class EcsEcr: raise def create_repository(self, registry_id, name): + if registry_id: + default_registry_id = self.sts.get_caller_identity().get('Account') + if registry_id != default_registry_id: + raise Exception('Cannot create repository in registry {}.' + 'Would be created in {} instead.'.format( + registry_id, default_registry_id)) if not self.check_mode: - repo = self.ecr.create_repository( - repositoryName=name, **build_kwargs(registry_id)).get( - 'repository') + repo = self.ecr.create_repository(repositoryName=name).get('repository') self.changed = True return repo else: diff --git a/test/integration/targets/ecs_ecr/tasks/main.yml b/test/integration/targets/ecs_ecr/tasks/main.yml index b21c291bf1..6e95f40683 100644 --- a/test/integration/targets/ecs_ecr/tasks/main.yml +++ b/test/integration/targets/ecs_ecr/tasks/main.yml @@ -1,6 +1,6 @@ --- - set_fact: - ecr_name: 'ecr-test-{{ ansible_date_time.epoch }}' + ecr_name: '{{ resource_prefix }}-ecr' - block: