forked from ansible-collections/amazon.aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ec2_ami: Add support for params BootMode, TpmSupport, UefiData (ansib…
…le-collections#1037) ec2_ami: Add support for params BootMode, TpmSupport, UefiData SUMMARY Depends-On: ansible-collections#1066 Added support for params BootMode, TpmSupport, UefiData in ec2_ami. Fixes ansible-collections#944 ISSUE TYPE Feature Pull Request COMPONENT NAME ec2_ami ADDITIONAL INFORMATION Example playbook - name: abc hosts: localhost gather_facts: false tasks: - name: AMI Creation with boot_mode and tpm_support amazon.aws.ec2_ami: name: ami-create-test_legacy-bios state: present architecture: x86_64 virtualization_type: hvm root_device_name: /dev/sda1 device_mapping: - device_name: /dev/sda1 snapshot_id: snap-xxxxxxxxx wait: yes region: us-east-2 boot_mode: legacy-bios tpm_support: v2.0 tags: name: ami-create-test Reviewed-by: Gonéri Le Bouder <[email protected]> Reviewed-by: Mandar Kulkarni <[email protected]> Reviewed-by: Mike Graves <[email protected]>
- Loading branch information
1 parent
67bc04d
commit 9e07cec
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
changelogs/fragments/1037-ec2_ami-add-support-for-boot_mode-tpm_support-uefi_data.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- ec2_ami - add support for BootMode, TpmSupport, UefiData params (https://github.com/ansible-collections/amazon.aws/pull/1037). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,27 @@ | |
description: | ||
- Set to simple to enable enhanced networking with the Intel 82599 Virtual Function interface for the AMI and any instances that you launch from the AMI. | ||
type: str | ||
boot_mode: | ||
description: | ||
- The boot mode of the AMI. | ||
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ami-boot.html). | ||
type: str | ||
choices: ['legacy-bios', 'uefi'] | ||
tpm_support: | ||
description: | ||
- Set to v2.0 to enable Trusted Platform Module (TPM) support. | ||
- If the image is configured for NitroTPM support, the value is v2.0 . | ||
- Requires I(boot_mode) to be set to 'uefi'. | ||
- Requires an instance type that is compatible with Nitro. | ||
- Requires minimum botocore version 1.26.0. | ||
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nitrotpm.html). | ||
type: str | ||
uefi_data: | ||
description: | ||
- Base64 representation of the non-volatile UEFI variable store. | ||
- Requires minimum botocore version 1.26.0. | ||
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/uefi-secure-boot.html). | ||
type: str | ||
author: | ||
- "Evan Duffield (@scicoin-project) <[email protected]>" | ||
- "Constantin Bugneac (@Constantin07) <[email protected]>" | ||
|
@@ -216,6 +237,22 @@ | |
- device_name: /dev/sdb | ||
no_device: true | ||
- name: AMI Creation with boot_mode and tpm_support | ||
amazon.aws.ec2_ami: | ||
name: newtest | ||
state: present | ||
architecture: x86_64 | ||
virtualization_type: hvm | ||
root_device_name: /dev/sda1 | ||
device_mapping: | ||
- device_name: /dev/sda1 | ||
snapshot_id: "{{ snapshot_id }}" | ||
wait: yes | ||
region: us-east-1 | ||
boot_mode: uefi | ||
uefi_data: data_file.bin | ||
tpm_support: v2.0 | ||
- name: Deregister/Delete AMI (keep associated snapshots) | ||
amazon.aws.ec2_ami: | ||
image_id: "{{ instance.image_id }}" | ||
|
@@ -441,6 +478,12 @@ def create_image(module, connection): | |
billing_products = module.params.get('billing_products') | ||
ramdisk_id = module.params.get('ramdisk_id') | ||
sriov_net_support = module.params.get('sriov_net_support') | ||
boot_mode = module.params.get('boot_mode') | ||
tpm_support = module.params.get('tpm_support') | ||
uefi_data = module.params.get('uefi_data') | ||
|
||
if tpm_support and boot_mode != 'uefi': | ||
module.fail_json(msg="To specify 'tpm_support', 'boot_mode' must be 'uefi'.") | ||
|
||
if module.check_mode: | ||
image = connection.describe_images(Filters=[{'Name': 'name', 'Values': [str(name)]}]) | ||
|
@@ -509,6 +552,12 @@ def create_image(module, connection): | |
params['KernelId'] = kernel_id | ||
if root_device_name: | ||
params['RootDeviceName'] = root_device_name | ||
if boot_mode: | ||
params['BootMode'] = boot_mode | ||
if tpm_support: | ||
params['TpmSupport'] = tpm_support | ||
if uefi_data: | ||
params['UefiData'] = uefi_data | ||
image_id = connection.register_image(aws_retry=True, **params).get('ImageId') | ||
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: | ||
module.fail_json_aws(e, msg="Error registering image") | ||
|
@@ -731,6 +780,9 @@ def main(): | |
sriov_net_support=dict(), | ||
tags=dict(type='dict', aliases=['resource_tags']), | ||
purge_tags=dict(type='bool', default=True), | ||
boot_mode=dict(type='str', choices=['legacy-bios', 'uefi']), | ||
tpm_support=dict(type='str'), | ||
uefi_data=dict(type='str'), | ||
) | ||
|
||
module = AnsibleAWSModule( | ||
|
@@ -746,6 +798,9 @@ def main(): | |
if not any([module.params['image_id'], module.params['name']]): | ||
module.fail_json(msg="one of the following is required: name, image_id") | ||
|
||
if any([module.params['tpm_support'], module.params['uefi_data']]): | ||
module.require_botocore_at_least('1.26.0', reason='required for ec2.register_image with tpm_support or uefi_data') | ||
|
||
connection = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff()) | ||
|
||
if module.params.get('state') == 'absent': | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
dependencies: | ||
- setup_ec2_facts | ||
- role: setup_botocore_pip | ||
vars: | ||
botocore_version: '1.26.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# This file is part of Ansible | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from unittest.mock import MagicMock, Mock, patch, call | ||
|
||
import pytest | ||
|
||
from ansible_collections.amazon.aws.plugins.modules import ec2_ami | ||
|
||
module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_ami" | ||
|
||
|
||
@patch(module_name + ".get_image_by_id") | ||
def test_create_image_uefi_data(m_get_image_by_id): | ||
module = MagicMock() | ||
connection = MagicMock() | ||
|
||
m_get_image_by_id.return_value = { | ||
"ImageId": "ami-0c7a795306730b288", | ||
"BootMode": "uefi", | ||
"TpmSupport": "v2.0", | ||
} | ||
|
||
module.params = { | ||
"name": "my-image", | ||
"boot_mode": "uefi", | ||
"tpm_support": "v2.0", | ||
"uefi_data": "QU1aTlVFRkk9xcN0AAAAAHj5a7fZ9+3aT2gcVRgA8Ek3NipiPST0pCiCIlTJtj20FzENCcQa", | ||
} | ||
|
||
ec2_ami.create_image(module, connection) | ||
assert connection.register_image.call_count == 1 | ||
connection.register_image.assert_has_calls( | ||
[ | ||
call( | ||
aws_retry=True, | ||
Description=None, | ||
Name="my-image", | ||
BootMode="uefi", | ||
TpmSupport="v2.0", | ||
UefiData="QU1aTlVFRkk9xcN0AAAAAHj5a7fZ9+3aT2gcVRgA8Ek3NipiPST0pCiCIlTJtj20FzENCcQa" | ||
) | ||
] | ||
) |