I am using the latest version. You must refer the latest Ansible AWS EC2 module docs to fix any issues with the playbook. The examples at the bottom are very useful for quick reference.
I had to iterate many times before I got the playbook working. I had to create the VPC, subnet, security group, and then finally launch the EC2 instance. I had to use the wait flag to make sure the EC2 instance was created before I could use it.
SSH Connection Problem
This playbook for some reason does not work when connecting to the EC2 instance using a SSH key. When EC2 instance is created manually, I am able to connect to it using SSH. The code that uses boto3 Python SDK for AWS does not have the SSH connection problem.
Ansible is useful for those who have some devops experience. As a developer I find boto3 to be easier to use than Ansible. Ansible internally uses boto3. The advantage of using boto3 is the flexibility. However you have to write code to make it idempotent. Ansible is idempotent by default. You also have to manually cleanup the resources when you iterate on your boto3 project. By default you can only have 5 VPCs.
Here is the boto3 project that I am using to experiment with EC2. It can create an EC2 instance, assign a key pair and I am able to SSH into EC2 using that key pair.
Why Python SDK instead of Ruby SDK? The main reason is that the community is bigger and it has more momentum. You can see it in the github repo. I am no longer sticking to one favorite programming language. Main reason is that AI tools like Github Copilot, Claude and ChatGPT make it easier to experiment in any language. I am no longer bogged down by syntax of any language.