# ec2\_snapshot question: how to run only when needed?

**URL:** <https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593>\
**Category:** Ansible Project\
**Created:** [October 23, 2014, 6:37pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593 "2014-10-23T18:37:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [October 23, 2014, 6:37pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/1 "2014-10-23T18:37:06Z")

</div>

I'd like to snapshot EC2 volumes only when needed. I couldn't find an  
easy solution that didn't require calling out to boto or other tools.  
If it exists, please ignore the following 🙂 I wanted to ask here  
before opening a feature or pull request.

It could look like this:

- local\_action:  
&nbsp;&nbsp;&nbsp;&nbsp;module: ec2\_snapshot  
&nbsp;&nbsp;&nbsp;&nbsp;region: "{{ region }}"  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_id: "{{ id }}"  
&nbsp;&nbsp;&nbsp;&nbsp;device\_name: "{{ device }}"  
&nbsp;&nbsp;&nbsp;&nbsp;snapshot\_tags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SnapshotCreator: ansible  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Host: "{{ server }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Device: "{{ device }}"  
&nbsp;&nbsp;&nbsp;&nbsp;# new feature, using the snapshot\_tags to determine current state  
&nbsp;&nbsp;&nbsp;&nbsp;# currently it's "always"  
&nbsp;&nbsp;&nbsp;&nbsp;state: present|absent|always

Does this make sense? Currently it's not available but could be  
implemented in a backwards compatible way if the default state was  
"always."

Thanks  
Ted

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [October 23, 2014, 7:03pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/2 "2014-10-23T19:03:22Z")

</div>

Alternate suggestion for this new behavior: copy the ec2 module's count  
options. It would add these options to ec2\_snapshot.py:

...  
&nbsp;&nbsp;exact\_count:  
&nbsp;&nbsp;&nbsp;&nbsp;version\_added: "1.8"  
&nbsp;&nbsp;&nbsp;&nbsp;description:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- An integer value which indicates how many snapshots that match the 'count\_tag' parameter should be present. Snapshots are either created or terminated based on this value.  
&nbsp;&nbsp;&nbsp;&nbsp;required: false  
&nbsp;&nbsp;&nbsp;&nbsp;default: null  
&nbsp;&nbsp;&nbsp;&nbsp;aliases:   
&nbsp;&nbsp;count\_tag:  
&nbsp;&nbsp;&nbsp;&nbsp;version\_added: "1.8"  
&nbsp;&nbsp;&nbsp;&nbsp;description:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Used with 'exact\_count' to determine how many snapshots based on a specific tag criteria should be present.  
&nbsp;&nbsp;&nbsp;&nbsp;required: false  
&nbsp;&nbsp;&nbsp;&nbsp;default: null  
&nbsp;&nbsp;&nbsp;&nbsp;aliases:

So when `exact_count` is omitted, things work like they do now, but if  
it's specified, an `enforce_count()` function will be called.

Ted

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [October 23, 2014, 8:28pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/3 "2014-10-23T20:28:16Z")

</div>

...

Final suggestion 🙂

boto has a nice `trim_snapshots()` method [1] that could simply be  
called inline, given appropriate parameters from the ec2\_snapshot  
module. I could use that too, but it seems much less flexible than the  
ec2 module's exact\_count/count\_tag approach.

Thanks!  
Ted

[1] [http://boto.readthedocs.org/en/latest/ref/ec2.html#boto.ec2.connection.EC2Connection.trim\_snapshots](http://boto.readthedocs.org/en/latest/ref/ec2.html#boto.ec2.connection.EC2Connection.trim_snapshots)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 24, 2014, 1:32pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/4 "2014-10-24T13:32:30Z")

</div>

Hi Ted,

We’d be happy to consider a pull request to ansible/ansible-modules-core

(We would like to avoid cluttering up the tracker with feature requests as usually folks are unlikely to scratch itches of others)

ansible-devel list is the right forum to discuss what form this maybe should take.

Thanks!

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [October 24, 2014, 3:25pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/5 "2014-10-24T15:25:19Z")

</div>

> We'd be happy to consider a pull request to ansible/ansible-modules-core

All right, as soon as I know what direction you prefer 🙂

> (We would like to avoid cluttering up the tracker with feature requests as  
> usually folks are unlikely to scratch itches of others)

That's fine, I already started developing one version, using  
exact\_count/count\_tag. I just don't want to go too far and get the PR rejected.

> ansible-devel list is the right forum to discuss what form this maybe  
> should take.

All right, following up there. I hope the original thread is clear from  
what I quoted. My personal preference is to implement  
exact\_count/count\_tag like the ec2 module does, but hooking into the  
boto module's `trim_snapshots()` method is really effortless.

...ph, and maybe even then `trim_snapshots()` could be used through a  
new ec2\_snapshot\_trim module. But that seems really trivial, since it's  
just a call to python in the shell:

- shell:  
&nbsp;&nbsp;&nbsp;&nbsp;cmd: python -c 'import boto; import json; ec2\_conn = boto.connect\_ec2(); ec2\_conn.trim\_snapshots(8, 7, 4, True)'

Let me know...

Thanks  
Ted

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [November 3, 2014, 2:52pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/6 "2014-11-03T14:52:24Z")

</div>

Ping?

Ted

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 3, 2014, 3:00pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/7 "2014-11-03T15:00:33Z")

</div>

I don’t really have this specific use case, so it’s hard for me to weigh in.

I’d see what others have to say that do have this particular question, and maybe it’s a new param or a new module, or maybe it’s a custom module that you use, etc.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [November 3, 2014, 4:52pm UTC](https://forum.ansible.com/t/ec2-snapshot-question-how-to-run-only-when-needed/16593/8 "2014-11-03T16:52:22Z")

</div>

I had this problem too. I didn't solve the cleaning up of old snapshots issue, but I added a feature to ec2\_snapshot to only ceate a new snapshot if the last one is older than X minutes. My changes are in this PR if you're interested: [https://github.com/ansible/ansible-modules-core/pull/76](https://github.com/ansible/ansible-modules-core/pull/76)
