# HowTo run tasks only after a snapshot was successfully taken?

**URL:** <https://forum.ansible.com/t/howto-run-tasks-only-after-a-snapshot-was-successfully-taken/25972>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [November 9, 2017, 9:50am UTC](https://forum.ansible.com/t/howto-run-tasks-only-after-a-snapshot-was-successfully-taken/25972 "2017-11-09T09:50:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [November 9, 2017, 9:50am UTC](https://forum.ansible.com/t/howto-run-tasks-only-after-a-snapshot-was-successfully-taken/25972/1 "2017-11-09T09:50:54Z")

</div>

Hi @all,

let me explain my scenario first, before I come to my question.

We run most of our linux servers as virtual machines in VMware vSphere Clusters. I would like to use a playbook that takes a snapshot from a vm befor it runs further tasks on the target node. In case of a broken system after an upgrade I could revert the snapshot and I’m up and running again.

Here is a not working example playbook:

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 12, 2017, 2:23pm UTC](https://forum.ansible.com/t/howto-run-tasks-only-after-a-snapshot-was-successfully-taken/25972/2 "2017-11-12T14:23:47Z")

</div>

> let me explain my scenario first, before I come to my question.
> 
> We run most of our linux servers as virtual machines in VMware vSphere  
> Clusters. I would like to use a playbook that takes a snapshot from a vm  
> befor it runs further tasks on the target node. In case of a broken system  
> after an upgrade I could revert the snapshot and I'm up and running again.
> 
> Here is a not working example playbook:  
> ---  
> - hosts: localhost  
> &nbsp;&nbsp;connection: local

Instead of using hosts: localhost, you could use the inventory hostname or the group.  
Since you have connection: local it will run on the controller host.

The benefits is that if you use "register: some\_variable" then the variable will be registered on the host and not localhost.

> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: snapshot operation  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vmware\_guest\_snapshot:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ vc }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: "{{ vc\_user }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ vc\_passwd }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datacenter: "{{ vc\_datacenter }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;folder: /mydatacenter/vm/"{{ item.folder }}"/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.vm }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;snapshot\_name: snap1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description: "Fuegen Sie hier Ihre Beschreibung ein"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_item:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ nested\_list }}"

Then you could use "register: status\_snapshot" on this task, and check it's status in the next play.

> - hosts: staging  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: run yum -y update  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yum:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest

Variables in one play is available in subsequent play in the same playbook.  
So you can then use the variable status\_snapshot to decide what to do in this play.

> The group 'staging' in the second play contains the hosts that should be  
> updated after a snapshot was taken. In the example above ansible would at  
> first try to create snapshots and at second update my nodes whether the  
> snapshot creation was successful or not. I have no idea how to create a  
> dependency from the second play to the first one.
> 
> Do you understand what I'm trying to accomplish? Do someone have an idea on  
> how to solve this?

Yes it's understandable what you are trying to do, hopefully my suggestion give you a tip on how to achieve it.

---

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [November 12, 2017, 7:31pm UTC](https://forum.ansible.com/t/howto-run-tasks-only-after-a-snapshot-was-successfully-taken/25972/3 "2017-11-12T19:31:36Z")

</div>

> > Here is a not working example playbook:  
> > ---  
> > - hosts: localhost  
> > &nbsp;&nbsp;connection: local
> 
> Instead of using hosts: localhost, you could use the inventory hostname or the group.  
> Since you have connection: local it will run on the controller host.
> 
> The benefits is that if you use "register: some\_variable" then the variable will be registered on the host and not localhost.
> 
> > &nbsp;&nbsp;tasks:  
> > &nbsp;&nbsp;&nbsp;&nbsp;- name: snapshot operation  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vmware\_guest\_snapshot:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ vc }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: "{{ vc\_user }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ vc\_passwd }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datacenter: "{{ vc\_datacenter }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;folder: /mydatacenter/vm/"{{ item.folder }}"/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.vm }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;snapshot\_name: snap1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description: "Fuegen Sie hier Ihre Beschreibung ein"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: false  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_item:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ nested\_list }}"
> 
> Then you could use "register: status\_snapshot" on this task, and check it's status in the next play.

Ok, I'll try that. Since one of the common return values of ansible  
modules is  
[failed]([http://docs.ansible.com/ansible/latest/common\_return\_values.html#id5](http://docs.ansible.com/ansible/latest/common_return_values.html#id5))  
I would like to use it in my play. But how could I use it? Do I simply  
have to use "register: failed" to check it in the next play?

> > - hosts: staging  
> > &nbsp;&nbsp;tasks:  
> > &nbsp;&nbsp;&nbsp;&nbsp;- name: run yum -y update  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yum:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "\*"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest
> 
> Variables in one play is available in subsequent play in the same playbook.  
> So you can then use the variable status\_snapshot to decide what to do in this play.

Could you give an example how to evaluate the variable, please?

Best regards,  
Joerg

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 13, 2017, 8:22pm UTC](https://forum.ansible.com/t/howto-run-tasks-only-after-a-snapshot-was-successfully-taken/25972/4 "2017-11-13T20:22:25Z")

</div>

> \>  
> \> Then you could use "register: status\_snapshot" on this task, and check it's status in the next play.
> 
> Ok, I'll try that. Since one of the common return values of ansible  
> modules is  
> [failed]([http://docs.ansible.com/ansible/latest/common\_return\_values.html#id5](http://docs.ansible.com/ansible/latest/common_return_values.html#id5))  
> I would like to use it in my play. But how could I use it? Do I simply  
> have to use "register: failed" to check it in the next play?

When a task fails the default behavior is that the playbook will stop for that host.  
So even if you have several play in the playbook they will not run that host if one of the task before has failed.

So you wouldn't need to do anything to avoid the second play to run as long as the snapshot is on the same server as the Ansible code is going to run.

But lets say you have a database server(db-server) and an application server(app-server).  
Before upgrading the app-server a snapshot of the db-server need to be taken so a rollback can be taken since the upgrade of app-server also changes the database template.  
In this kind of scenarios you'll need to do some checking.

But if you have disabled the holt on errors you can use this to check

- debug: msg="Will run if task failed"  
&nbsp;&nbsp;when: status\_snapshot.failed == true

> \>\> - hosts: staging  
> \>\> tasks:  
> \>\> - name: run yum -y update  
> \>\> yum:  
> \>\> name: "\*"  
> \>\> state: latest  
> \>  
> \> Variables in one play is available in subsequent play in the same playbook.  
> \> So you can then use the variable status\_snapshot to decide what to do in this play.
> 
> Could you give an example how to evaluate the variable, please?

- hosts: vm1,vm2,vm3  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Create random number from 1 to 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: shuf -i 1-2 -n 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result

- hosts: vm1,vm2,vm3  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ inventory\_hostname}} found the correct number, the number was 2"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: result.stdout == "2"

This will run the command shuf on all vms, but in the second task in the other play it will check the result of shuf.  
If shuf has returned the number 2 then the debug task will run on only that host, the other this task will be skipped.
