0

My use case is to use a single playbook to run the same role on 2 different host groups, but I want to pass different variable values depending on the host group the role is executing against. I've tried many ways of doing this, but I am finding the second play for the second host group is not executed.

---
- name: Run against webservers
  hosts: webservers
  vars: var1: "val_a"
  roles:
     - common_role

- name: Run against dbservers
  hosts: dbservers
  vars: var1: "val_b"
  roles:
     - common_role
```

when I execute code similar to above, it only executes the first play, against webservers, and terminates. Is my use case valid? (i.e. execution of same role, against 2 host groups, but passing different variable values for each group, within single playbook) 

joey_82
  • 41
  • 7
  • 2
    This is a common use case, and should work. Do you get any errors (when it terminates)? – seshadri_c Sep 15 '21 at 16:43
  • no i dont get any errors, only first set of hosts are played. – joey_82 Sep 15 '21 at 16:55
  • Do you get any output such as "skipping: no hosts matched" ? Are you sure your host dbservers exist in the inventory? – Unsel Sep 15 '21 at 18:07
  • the final task in the first play does fail expectedly, so the last output I see from the playbook execution is the Play Recap for the webserver hosts. It doesnt even try to execute the play on the dbservers. And yes, the dbservers are in the inventory file. Could the fact the final task for the webserver hosts fails be the reason it doesnt move onto the dbservers? I would have thought the failed task there would not impact on the rest of the playbook. – joey_82 Sep 15 '21 at 19:22
  • You can set the ignore_errors on the failing task to continue the execution. ```ignore_errors: true ``` – Unsel Sep 15 '21 at 19:44
  • so yes, the failing error on the first role play execution was preventing the second role play from running. I thought the second role should have played even with failing error on previous role execution, so my misunderstanding here – joey_82 Sep 16 '21 at 14:33

1 Answers1

0

You can set the ignore_errors on the failing task to continue the execution.

- fail:
  when: var1 == 'val_a'
  ignore_errors: true

But keep in mind that current role continue to execute rest of the tasks after failing one too.

Unsel
  • 343
  • 2
  • 8