are we allowed to tag a rescue block ?

451 views
Skip to first unread message

fanvalt

unread,
Apr 13, 2017, 8:03:17 AM4/13/17
to Ansible Project
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

Phonthip Namkaew

unread,
Apr 13, 2017, 10:00:04 AM4/13/17
to Ansible Project
Not tested:

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

when: do_restore is defined

With restore:
ansible-playbook myplaybook.yml --extra-vars="do_restore=True"

Without restore:
ansible-playbook myplaybook.yml

Phonthip Namkaew

unread,
Apr 13, 2017, 10:12:19 AM4/13/17
to Ansible Project
Not tested:
- tasks: restore the config file
shell: chdir="{{ dirsav }}" mv config /{{ Directoryname }}/
tags:
- restore

With restore:
ansible-playbook myplaybook.yml

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

fanvalt

unread,
Apr 13, 2017, 10:20:23 AM4/13/17
to Ansible Project
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

Brian Coca

unread,
Apr 13, 2017, 10:21:34 AM4/13/17
to Ansible Project
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.

----------
Brian Coca

fanvalt

unread,
Apr 19, 2017, 5:29:07 AM4/19/17
to Ansible Project
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,

fanvalt

unread,
Apr 19, 2017, 5:34:39 AM4/19/17
to Ansible Project
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

Philippe Eveque

unread,
Apr 19, 2017, 10:24:06 AM4/19/17
to ansible...@googlegroups.com
I would recommend to not use tag for conditional execution of part of a play/playbook
Just A personal preference

Phil

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1da6358a-3130-4070-9c32-d52a328d362a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages