Include_roles with block/always(rescue)

59 views
Skip to first unread message

ruv.d...@gmail.com

unread,
Oct 5, 2020, 11:43:51 AM10/5/20
to Ansible Project
Hi guys,

I have 3 roles:
 - role A: which needs to be executed on localhost
 - role B: which needs to run on a remote host
 - role C: which is a rescue role that cleans up some things which role A did on localhost (this one should always run, especially if some task failed in one of the other roles)

How could I achieve this?

The closest I've been was something like:

```
- hosts: all
  tasks:
    - block:
      - include_role:
        name: role A
     - include_role:
        name: role B
     always:
       - include_role:
        name: role C
```

but this of course has no posibilty of changing the hosts against which it is run ('localhost' is not 'all' being the problem)

Dick Visser

unread,
Oct 5, 2020, 12:03:35 PM10/5/20
to ansible...@googlegroups.com
This is one play. 
Add separate plays for localhost etc.


--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9b9d54eb-14a4-4a25-bb7c-2621c6aed25bn%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

ruv.d...@gmail.com

unread,
Oct 5, 2020, 2:37:43 PM10/5/20
to Ansible Project
Hi Dick,

True, that's a single play. But how could I achieve the block/always behavior with separate plays per host? The inventory is dynamically loaded, not an issue, but I don't know how to make sure that role C is executed on localhost no matter what.

Dick Visser

unread,
Oct 6, 2020, 3:46:27 AM10/6/20
to ansible...@googlegroups.com
This might work:


---
- hosts: all

tasks:
- name: my block
block:
- name: Local role
include_role:
name: roles/test1
apply:
delegate_to: localhost
- name: Remote role
include_role:
name: roles/test2
always:
- name: Another local role
include_role:
name: roles/test3
apply:
delegate_to: localhost
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/55be225d-0b41-4e7c-b870-ec0b170c3ae9n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Dorin RuV

unread,
Oct 6, 2020, 6:44:20 AM10/6/20
to ansible...@googlegroups.com
Hey Dick,

This seems to be exactly what I need, thanks a lot!

Right now I'm having issues on a different point:

```
- hosts: all
  tasks:
    - name: Role A
      block:
        - name: Do A
          include_role:
            name: roleA
            apply:
              delegate_to:
                localhost
        - name: Refresh Inventory
          meta: refresh_inventory
        - name: Do remote Role B
          include_role:
            name: roleB
      always:
        - name: Clean up
          include_role: 
            name: roleC
```

I have a custom inventory plugin which needs to be refreshed after role A. It seems though that the meta action interrupts execution there, not even the always block getting executed anymore. I'm opening an issue with Ansible on this.

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/ve_fFg3wgvM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwOj%3DpEDfStONqXPqF6aKAChu7pp%3Dkevv7gj6x0SXwXyYg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages