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

* removed additional check for name parameter

(cherry picked from commit 91357d07f4)

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

(cherry picked from commit 60a3277103)

* [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)
pull/39675/head
Maish 7 years ago committed by Ryan Brown
parent eaa9364159
commit 389db4c49e

@ -677,10 +677,14 @@ def main():
argument_spec=argument_spec, argument_spec=argument_spec,
required_if=[ required_if=[
['state', 'absent', ['image_id']], ['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: try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) 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) connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_kwargs)

@ -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 - name: create an image from the instance
ec2_ami: ec2_ami:
ec2_region: '{{ec2_region}}' ec2_region: '{{ec2_region}}'
@ -295,7 +319,6 @@
security_token: '{{security_token}}' security_token: '{{security_token}}'
state: present state: present
image_id: '{{ result.image_id }}' image_id: '{{ result.image_id }}'
name: '{{ ec2_ami_name }}_ami'
description: '{{ ec2_ami_description }}' description: '{{ ec2_ami_description }}'
tags: tags:
Name: '{{ ec2_ami_name }}_ami' Name: '{{ ec2_ami_name }}_ami'

Loading…
Cancel
Save