Here at Cloudar we’re using Ansible to automate deployments. We quickly realised its potential to orchestrate custom and more complicated automation
Recently we were looking for a way to automate the following actions.
- Creating snapshots
- Starting and stopping instances
- Removing old snapshots
We already had a script and cronjob to take regular snapshots, but we wanted to centralise our automation to improve visibility and logging.
While this is possible using the existing Ansible modules, we needed a way to specify multiple parameters on a per instance basis. Specifically we needed it to be possible to:
- Change the start and stop times from within the AWS Console
- Allow users without access to (or knowledge of) Ansible to prevent the automatic shutdown of a machine
We quickly decided to use tags to specify these parameters. Because there’s a limit of ten tags per instance, and we wanted to store other information (like Environment and Project) too, it wasn’t possible to store each parameter in a separate tag.
We solved this by writing our own Ansible modules. Each module reads the same tag, where we store a JSON dictionary with all the needed variables. This way we keep room for other tags, while allowing everyone with the right to create tags to change things if they need to.
The source code is available on GitHub. You can install it by adding the path to your Ansible Library. Complete usage instructions and examples are included in the repository.
klemen
ever thought about contacting ansible author and merging (something like) this in ansible?
Ben Bridts
Hi Klemen,
I think that these modules are a bit too specific to our workflow to be a good part of the Ansible Extra Modules, so we didn’t reach out to the Ansible team.
That being said, it would be very nice to get this shipped with Ansbile.
prasan
hey, I am getting following error while i am running this code. Any idea? am i missing anything?
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File “/tmp/ansible-tmp-1463012517.56-174809243965973/cat_create_snapshot”, line 2393, in
main()
File “/tmp/ansible-tmp-1463012517.56-174809243965973/cat_create_snapshot”, line 86, in main
if grace_minutes.isdigit():
AttributeError: ‘int’ object has no attribute ‘isdigit’
—
– name: Snap
hosts: tag_Backup_true
connection: local
gather_facts: false
tasks:
– name: Create Snapshot
local_action:
module: cat_create_snapshot
tag: CAT
grace: 10
Ben Bridts
Hey Prasan,
Are you using Ansible 2.x? This module was written for ansible 1.9.x and the way parameters are passed may have changed. Can you try with using grace: ’10’ (inside quotes) or not changing the grace time?
Kind regards,
Ben