We want to use Ansible to create a set of volumes based on an instance’s latest snapshot. Doing this requires a few queries, and I didn’t see anything that provided the functionality I was looking for, so I wrote a new module, named ec2_getall, which I’ve attached here (zipped because Google Groups didn’t allow it directly).
ec2_getall provides a thin wrapper around a boto EC2Connection instance, allowing access to its get_all_* methods. It takes the standard ec2 arguments (e.g., keys and region) as well as a name argument that specifies which (“get_all_” + name) to call. Any other arguments are passed as keyword arguments to the get_all method.
Example usage:
- hosts: localhost
vars_files: - vars/aws-credentials.yml
tasks: - name: Get all instance Name tags
ec2_getall:
name: tags
region: us-east-1
aws_access_key: “{{ aws_access_key }}”
aws_secret_key: “{{ aws_secret_key }}”
filters:
key: Name
resource_type: instance
register: ec2 - debug: var=ec2
This is effectively the same as creating an ec2 connection and then calling:
ec2.get_all_tags(filters=dict(key=“Name”, resource_type=“instance”))
The debug output:
“ec2”: {
“changed”: true,
“data”: [
{
“item”: "\n ",
“name”: “Name”,
“res_id”: “i-xxxxxxxx”,
“res_type”: “instance”,
“value”: “XXX”
},
{
“item”: "\n ",
“name”: “Name”,
“res_id”: “i-yyyyyyyy”,
“res_type”: “instance”,
“value”: “YYY”
},
… # remaining tag JSON omitted
],
“invocation”: {
“module_args”: “”,
“module_name”: “ec2_getall”
}
}
If there is general interest in this module, I will be happy to document it, address any concerns, and create a Pull Request.
Best,
Brandon
(attachments)
ec2_getall.zip (837 Bytes)