# are we allowed to tag a rescue block ?

**URL:** <https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218>\
**Category:** Ansible Project\
**Created:** [April 13, 2017, 12:03pm UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218 "2017-04-13T12:03:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [April 13, 2017, 12:03pm UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/1 "2017-04-13T12:03:17Z")

</div>

hello,

I have a playbook running well, but I want to be able to restore files if it fails.  
So I want to add a block/rescue, I am allowed to tag the rescue so the restore of files will only run if I run my playbook with the --tags=“restore” ?  
Or do I Have to tag all the tasks in the rescue block ?

here is an example:

- block:

- tasks: save the config file  
shell: chdir=/{{ Directoryname }} mv config {{ dirsav }}/

- rescue:

- tasks: restore the config file  
shell: chdir=“{{ dirsav }}” mv config /{{ Directoryname }}/

tags:

- restore

Regards

---

<div class="post-metadata">

**Author:** ![Phonthip\_Namkaew](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Phonthip\_Namkaew](https://forum.ansible.com/u/Phonthip_Namkaew)\
**Post date:** [April 13, 2017, 2:00pm UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/2 "2017-04-13T14:00:04Z")

</div>

Not tested:

&nbsp;&nbsp;- tasks: restore the config file  
&nbsp;&nbsp;&nbsp;&nbsp;shell: chdir="{{ dirsav }}" mv config /{{ Directoryname }}/  
&nbsp;&nbsp;&nbsp;&nbsp;when: do\_restore is defined

With restore:  
ansible-playbook myplaybook.yml --extra-vars="do\_restore=True"

Without restore:  
ansible-playbook myplaybook.yml

---

<div class="post-metadata">

**Author:** ![Phonthip\_Namkaew](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Phonthip\_Namkaew](https://forum.ansible.com/u/Phonthip_Namkaew)\
**Post date:** [April 13, 2017, 2:12pm UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/3 "2017-04-13T14:12:19Z")

</div>

Not tested:  
&nbsp;&nbsp;- tasks: restore the config file  
&nbsp;&nbsp;&nbsp;&nbsp;shell: chdir="{{ dirsav }}" mv config /{{ Directoryname }}/  
&nbsp;&nbsp;&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- restore

With restore:  
ansible-playbook myplaybook.yml

Without restore:  
ansible-playbook myplaybook.yml --skip-tags "restore"

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [April 13, 2017, 2:20pm UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/4 "2017-04-13T14:20:23Z")

</div>

thank you but my question is about the rescue reserved word ?  
Is it possible to run the rescue block with a tag instead of tagging all tasks in the rescue block ?

Regards

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 13, 2017, 2:21pm UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/5 "2017-04-13T14:21:28Z")

</div>

You can tag the whole block or individual tasks within the block,  
there is no way to only tag the rescue/always part of a block.

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [April 19, 2017, 9:29am UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/6 "2017-04-19T09:29:07Z")

</div>

Thank you, finally i cannot figure out how to work on my playbook.

I have a main.yml with some includes, some are tags to skip them with the --skip-tags option.

main.yml:

- include: existence.yml

…

- include: installation.yml  
tags:
- installation

…

- include: demarrage\_karaf.yml

so if I use --skip-tags “installation”, this will only restart the karaf instance.  
If I don’t use any tags, the installation will be done and karaf will be restarted

What I want is to add a restore option (tags) so if installation or any tasks failed, I can restore the old installation and restart karaf.

I thought adding the main in a block rescue this way would work:

block:

- include: existence.yml  
…

- include: installation.yml  
tags:

- installation

…

- include: demarrage\_karaf.yml

rescue:

- include: failure.yml  
tags:
- restore

but if I run the playbook with only the --tags “restore” , it won’t run any tasks not tagged or the include tagged with the “installation” tags.  
Shall I tag all tasks with “always” tag ?  
Is there another way to proceed than using a restore tag ?

Regards,

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [April 19, 2017, 9:34am UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/7 "2017-04-19T09:34:38Z")

</div>

instead of tagging --tags “restore” when I want to restore, I guess the way to proceed is to use --skip-tags “restore” if I don’t want to restore

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [April 19, 2017, 2:23pm UTC](https://forum.ansible.com/t/are-we-allowed-to-tag-a-rescue-block/24218/8 "2017-04-19T14:23:44Z")

</div>

I would recommend to not use tag for conditional execution of part of a play/playbook  
Just A personal preference

Phil
