How to execute tasks based on group names in ansible

Hi,

I would like to use the when condition to execute tasks based on group names in ansible.

I am using this code snipit

when: ({{group_names}} == "web-server")

But it always skips.

Can someone please correct me.

when: ‘“web-server” in group_names’

group_names is an array/list so you have to check if ‘web-server’ exists within it.

Have you though about creating another play and just including those tasks under that play?

  • hosts: all
    tasks:

  • name: do task A

  • hosts: webservers
    tasks:

  • name: do tasks A only for webservers

I maybe oversimplifying your workflow but it could help decouple your tasks for re-usability later on.

(attachments)