Skip to content

Commit

Permalink
ec2_launch_template: implement missing metadata options (ansible-coll…
Browse files Browse the repository at this point in the history
…ections#917)

ec2_launch_template: implement missing metadata options

SUMMARY
Add missing metadata options

instance_metadata_tags
http_protocol_ipv6

ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_launch_template

Reviewed-by: Markus Bergholz <[email protected]>
Reviewed-by: Mark Woolley <[email protected]>
Reviewed-by: Alina Buzachis <None>
  • Loading branch information
markuman authored Feb 3, 2022
1 parent 0e08a40 commit d47e188
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_launch_template - Add metadata options parameter ``http_protocol_ipv6`` and ``instance_metadata_tags`` (https://github.com/ansible-collections/community.aws/pull/917).
38 changes: 37 additions & 1 deletion plugins/modules/ec2_launch_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@
The state of token usage for your instance metadata requests.
choices: [optional, required]
default: 'optional'
http_protocol_ipv6:
version_added: 3.1.0
type: str
description: >
- Wether the instance metadata endpoint is available via IPv6 (C(enabled)) or not (C(disabled)).
- Requires boto3 >= 1.18.29
choices: [enabled, disabled]
default: 'disabled'
instance_metadata_tags:
version_added: 3.1.0
type: str
description:
- Wether the instance tags are availble (C(enabled)) via metadata endpoint or not (C(disabled)).
- Requires boto3 >= 1.20.30
choices: [enabled, disabled]
default: 'disabled'
'''

EXAMPLES = '''
Expand Down Expand Up @@ -516,6 +532,24 @@ def create_or_update(module, template_options):
out = {}
lt_data = params_to_launch_data(module, dict((k, v) for k, v in module.params.items() if k in template_options))
lt_data = scrub_none_parameters(lt_data, descend_into_lists=True)

if lt_data.get('MetadataOptions'):
if not module.boto3_at_least('1.20.30'):
# fail only if enabled is requested
if lt_data['MetadataOptions'].get('InstanceMetadataTags') == 'enabled':
module.require_boto3_at_least('1.20.30', reason='to set instance_metadata_tags')
# pop if it's not requested to keep backwards compatibility.
# otherwise the modules failes because parameters are set due default values
lt_data['MetadataOptions'].pop('InstanceMetadataTags')

if not module.boto3_at_least('1.18.29'):
# fail only if enabled is requested
if lt_data['MetadataOptions'].get('HttpProtocolIpv6') == 'enabled':
module.require_boto3_at_least('1.18.29', reason='to set http_protocol_ipv6')
# pop if it's not requested to keep backwards compatibility.
# otherwise the modules failes because parameters are set due default values
lt_data['MetadataOptions'].pop('HttpProtocolIpv6')

if not (template or template_versions):
# create a full new one
try:
Expand Down Expand Up @@ -671,7 +705,9 @@ def main():
options=dict(
http_endpoint=dict(choices=['enabled', 'disabled'], default='enabled'),
http_put_response_hop_limit=dict(type='int', default=1),
http_tokens=dict(choices=['optional', 'required'], default='optional')
http_tokens=dict(choices=['optional', 'required'], default='optional'),
http_protocol_ipv6=dict(choices=['disabled', 'enabled'], default='disabled'),
instance_metadata_tags=dict(choices=['disabled', 'enabled'], default='disabled'),
)
),
network_interfaces=dict(
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/ec2_launch_template/meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ dependencies:
- prepare_tests
- setup_ec2
- setup_remote_tmp_dir
- role: setup_botocore_pip
vars:
boto3_version: "1.20.30"
Original file line number Diff line number Diff line change
@@ -1,24 +1,78 @@
- block:
- name: metadata_options
ec2_launch_template:
name: "{{ resource_prefix }}-test-metadata"
metadata_options:
http_put_response_hop_limit: 1
http_tokens: required
state: present
register: metadata_options_launch_template
- name: instance with metadata_options created with the right options
assert:
that:
- metadata_options_launch_template is changed
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_put_response_hop_limit == 1"
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_tokens == 'required'"
---
- name: test with older boto3 version that does not support instance_metadata_tags
block:
- name: fail metadata_options
ec2_launch_template:
name: "{{ resource_prefix }}-test-metadata"
metadata_options:
http_put_response_hop_limit: 1
http_tokens: required
http_protocol_ipv6: enabled
instance_metadata_tags: enabled
state: present
register: metadata_options_launch_template
ignore_errors: yes
- name: verify fail with usefull error message
assert:
that:
- metadata_options_launch_template.failed
- metadata_options_launch_template is not changed
- "'This is required to set instance_metadata_tags' in metadata_options_launch_template.msg"

- name: success metadata_options
ec2_launch_template:
name: "{{ resource_prefix }}-test-metadata"
metadata_options:
http_put_response_hop_limit: 1
http_tokens: required
state: present
register: metadata_options_launch_template
- name: instance with metadata_options created with the right options
assert:
that:
- metadata_options_launch_template is changed
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_put_response_hop_limit == 1"
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_tokens == 'required'"
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_protocol_ipv6 is not defined"
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.instance_metadata_tags is not defined"
always:
- name: delete the template
ec2_launch_template:
name: "{{ resource_prefix }}-test-metadata"
state: absent
register: del_lt
retries: 10
until: del_lt is not failed
ignore_errors: true
- name: delete the template
ec2_launch_template:
name: "{{ resource_prefix }}-test-metadata"
state: absent
register: del_lt
retries: 10
until: del_lt is not failed
ignore_errors: true

- name: test with boto3 version that supports instance_metadata_tags
vars:
ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}"
block:
- name: metadata_options
ec2_launch_template:
name: "{{ resource_prefix }}-test-metadata"
metadata_options:
http_put_response_hop_limit: 1
http_tokens: required
http_protocol_ipv6: enabled
instance_metadata_tags: enabled
state: present
register: metadata_options_launch_template
- name: instance with metadata_options created with the right options
assert:
that:
- metadata_options_launch_template is changed
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_put_response_hop_limit == 1"
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_tokens == 'required'"
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.http_protocol_ipv6 == 'enabled'"
- "metadata_options_launch_template.latest_template.launch_template_data.metadata_options.instance_metadata_tags == 'enabled'"
always:
- name: delete the template
ec2_launch_template:
name: "{{ resource_prefix }}-test-metadata"
state: absent
register: del_lt
retries: 10
until: del_lt is not failed
ignore_errors: true

0 comments on commit d47e188

Please sign in to comment.