Ansible: Migrating from ec2.py to aws_ec2

`ec2.py` was deprecated and replaced with `aws_ec2` for Ansible in 2021. This shows you how to replace the old script with the new Ansible plugin.

Ansible: Migrating from ec2.py to aws_ec2

I very recently had to update a project to use the new aws_ec2 plugin, after the ec2.py script stopped working in Ansible 2.10. If you (like me), just used the python script just for for EC2 inventory, then the following template for your aws_ec2 configuration file should work.

plugin: aws_ec2
regions:
  - us-east-1
  - us-east-2
  - us-west-1
  - ap-southeast-1
  - eu-central-1
  - eu-west-2
filters:
  instance-state-name: running
hostnames:
  - private-ip-address
keyed_groups:
  - prefix: tag
    key: tags

Add the template into your top-level inventories file. Then in the ansible.cfg file you will want to enable the plugin for the project. This can be done with the following line:

[defaults]
enable_plugins = aws_ec2

Finally, the most important thing (for me) was that the current region is no longer ec2_region. The new variable is placement.region. Once you've addded the two files and updated your usages of ec2_region, you're good to go!

This took me a few hours to figure out fully, so I hope I've saved you some time!