[aws] Added empty default for ec2_ami name parameter (#38585)

* removed additional check for name parameter

(cherry picked from commit 91357d07f4f9eb3714f582c1959ff54d25129500)

* Added empty default
Fix for issue https://github.com/ansible/ansible/issues/38482

(cherry picked from commit 60a32771039c190a7e9f6e0eb4d0f6bf4c356fd4)

* [ec2_ami] Ensure name or image_id is provided for state=present (#38972)

Add integration tests for backward compatibility and ensuring name or image_id is provided
(cherry picked from commit e2aa1155ba)
This commit is contained in:
Maish 2018-05-03 15:03:11 +03:00 committed by Ryan Brown
parent eaa9364159
commit 389db4c49e
2 changed files with 29 additions and 2 deletions

View file

@ -677,10 +677,14 @@ def main():
argument_spec=argument_spec,
required_if=[
['state', 'absent', ['image_id']],
['state', 'present', ['name']],
]
)
# Using a required_one_of=[['name', 'image_id']] overrides the message that should be provided by
# the required_if for state=absent, so check manually instead
if not any([module.params['image_id'], module.params['name']]):
module.fail_json(msg="one of the following is required: name, image_id")
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_kwargs)

View file

@ -87,6 +87,30 @@
# ============================================================
- name: test clean failure if not providing image_id or name with state=present
ec2_ami:
ec2_region: '{{ec2_region}}'
ec2_access_key: '{{ec2_access_key}}'
ec2_secret_key: '{{ec2_secret_key}}'
security_token: '{{security_token}}'
instance_id: '{{ setup_instance.instance_ids[0] }}'
state: present
description: '{{ ec2_ami_description }}'
tags:
Name: '{{ ec2_ami_name }}_ami'
wait: yes
root_device_name: /dev/xvda
register: result
ignore_errors: yes
- name: assert error message is helpful
assert:
that:
- result.failed
- "result.msg == 'one of the following is required: name, image_id'"
# ============================================================
- name: create an image from the instance
ec2_ami:
ec2_region: '{{ec2_region}}'
@ -295,7 +319,6 @@
security_token: '{{security_token}}'
state: present
image_id: '{{ result.image_id }}'
name: '{{ ec2_ami_name }}_ami'
description: '{{ ec2_ami_description }}'
tags:
Name: '{{ ec2_ami_name }}_ami'