Retrieve Latest AMI ID
Create the Playbook
Create a playbook find_latest_ami.yml
to get the latest Ubuntu 22.04 AMI ID.
Run the Playbook
From the project root:
Trouble Shooting Issues
Connection Problem
Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused.
Change the connection parameter connection to local in the playbook:
Run the playbook.
Unsupported Parameters
Error: Unsupported parameters for (amazon.aws.ec2_ami_info) module.
Use Jinja2 filters to sort the images by creation date and select the last one.
Verify the AMI ID
You can verify that the AMI ID is correct by logging into the AWS console and searching for the AMI ID. Go to EC2 dashboard and click on the AMI Catalog. Select the Community AMIs tab and search for ami-0e21465cede02fd1e. You can see the creation date also matches.
You can also use AWS CLI to verify from the command line:
Output: