Skip to content

Commit

Permalink
EC2_instance: add support for controlling metadata options (#414)
Browse files Browse the repository at this point in the history
EC2_instance: add support for controlling metadata options

SUMMARY

Adding support for controlling the metadata options, 'Metadata Accessible' and 'Metadata Version' in amazon.aws.ec2_instance.

Fixes #399
ISSUE TYPE


Feature Pull Request

COMPONENT NAME

amazon.aws.ec2_instance

Reviewed-by: Jill R <None>
Reviewed-by: Abhijeet Kasurde <None>
Reviewed-by: Mark Chappell <None>
Reviewed-by: None <None>
  • Loading branch information
mandar242 authored Aug 19, 2021
1 parent 535f9d0 commit 09a8535
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_instance - add support for controlling metadata options (https://github.com/ansible-collections/amazon.aws/pull/414).
43 changes: 43 additions & 0 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,29 @@
description:
- The placement group that needs to be assigned to the instance
type: str
metadata_options:
description:
- Modify the metadata options for the instance.
- See U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more information.
- The two suboptions I(http_endpoint) and I(http_tokens) are supported.
type: dict
version_added: 2.0.0
suboptions:
http_endpoint:
description:
- Enables or disables the HTTP metadata endpoint on instances.
- If specified a value of disabled, metadata of the instance will not be accessible.
choices: [enabled, disabled]
default: enabled
type: str
http_tokens:
description:
- Set the state of token usage for instance metadata requests.
- If the state is optional (v1 and v2), instance metadata can be retrieved with or without a signed token header on request.
- If the state is required (v2), a signed token header must be sent with any instance metadata retrieval requests.
choices: [optional, required]
default: optional
type: str
extends_documentation_fragment:
- amazon.aws.aws
Expand Down Expand Up @@ -383,6 +406,17 @@
tags:
Env: "eni_on"
instance_type: t2.micro
- name: start an instance with metadata options
amazon.aws.ec2_instance:
name: "public-metadataoptions-instance"
vpc_subnet_id: subnet-5calable
instance_type: t3.small
image_id: ami-123456
tags:
Environment: Testing
metadata_options:
http_endpoint: enabled
http_tokens: optional
'''

RETURN = '''
Expand Down Expand Up @@ -1193,6 +1227,12 @@ def build_top_level_options(params):
spec['CpuOptions'] = {}
spec['CpuOptions']['ThreadsPerCore'] = params.get('cpu_options').get('threads_per_core')
spec['CpuOptions']['CoreCount'] = params.get('cpu_options').get('core_count')
if params.get('metadata_options'):
spec['MetadataOptions'] = {}
spec['MetadataOptions']['HttpEndpoint'] = params.get(
'metadata_options').get('http_endpoint')
spec['MetadataOptions']['HttpTokens'] = params.get(
'metadata_options').get('http_tokens')
return spec


Expand Down Expand Up @@ -1735,6 +1775,9 @@ def main():
instance_ids=dict(default=[], type='list', elements='str'),
network=dict(default=None, type='dict'),
volumes=dict(default=None, type='list', elements='dict'),
metadata_options=dict(type='dict', options=dict(
http_endpoint=dict(type='str', choices=['enabled', 'disabled'], default='enabled'),
http_tokens=dict(type='str', choices=['optional', 'required'], default='optional'))),
)
# running/present are synonyms
# as are terminated/absent
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_instance/inventory
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ebs_optimized
block_devices
cpu_options
metadata_options
default_vpc_tests
external_resource_attach
instance_no_wait
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
- block:
- name: "create t3.nano instance with metadata_options"
ec2_instance:
state: present
name: "{{ resource_prefix }}-test-t3nano-enabled-required"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
instance_type: t3.nano
metadata_options:
http_endpoint: enabled
http_tokens: required
wait: false
register: instance_creation

- name: "instance with metadata_options created with the right options"
assert:
that:
- instance_creation is success
- instance_creation is changed
- "'{{ instance_creation.spec.MetadataOptions.HttpEndpoint }}' == 'enabled'"
- "'{{ instance_creation.spec.MetadataOptions.HttpTokens }}' == 'required'"

- name: "modify metadata_options on existing instance"
ec2_instance:
state: present
name: "{{ resource_prefix }}-test-t3nano-enabled-required"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
instance_type: t3.nano
metadata_options:
http_endpoint: enabled
http_tokens: optional
wait: false
register: metadata_options_update
ignore_errors: yes

- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-t3nano-enabled-required"
register: presented_instance_fact

- name: "modify metadata_options has no effect on existing instance"
assert:
that:
- metadata_options_update is success
- metadata_options_update is not changed
- "{{ presented_instance_fact.instances | length }} > 0"
- "'{{ presented_instance_fact.instances.0.state.name }}' in ['running','pending']"
- "'{{ presented_instance_fact.instances.0.metadata_options.http_endpoint }}' == 'enabled'"
- "'{{ presented_instance_fact.instances.0.metadata_options.http_tokens }}' == 'required'"

always:
- name: "Terminate metadata_options instances"
ec2_instance:
state: absent
filters:
"tag:TestId": "{{ ec2_instance_tag_TestId }}"
wait: yes
ignore_errors: yes

0 comments on commit 09a8535

Please sign in to comment.