Avoid running pre/post tasks without roles/tasks

Hi all.

I have a master deployment playbook that more or less includes all of my servers, which I use with tags to deploy only the apps/configs I want at the time. Hosts are grouped into hostgroups by roles and each hostgroup has it’s own pre/post tasks for handling load-balancers, etc. It looks something like this:

  • hosts: tag_role_hostgroup1
    serial: 1
    pre_tasks:

  • name: Remove from lb

    tags: pre_tasks
    roles:

  • { role: app1, tags: app1 }

  • { role: app2, tags: app2 }
    post_tasks:

  • name: Register to lb

    tags: post_tasks

  • hosts: tag_role_hostgroup2
    serial: 1

pre_tasks:

  • name: Some preps

    tags: pre_tasks
    roles:
  • { role: app2, tags: app2 }
  • { role: app3, tags: app3 }
    post_tasks:
  • name: Cleanup

    tags: post_tasks

So far so good. It works pretty well as I can think about applications when deploying, and each hostgroup will take care of itself regarding load-balancers and all. Problem is when deploying for example app1 or app3. In this case one play will have no roles selected, but since I’m including “pre_tasks,post_tasks” as tags its pre/post tasks will still run, bringing machines down and up again with no work done.

Do you have an idea of how to tackle this? I prefer to not create a playbook per app as it’s a lot less powerful and flexible. I was thinking of making ansible run pre/post tasks only if some task in roles/tasks is going to run, but that’s kind of a last resort.

Thanks for Ansible, btw!

You could possibly consider passing ansible-playbook a --limit to only run on the servers you want to run on.

Others may have better suggestions.