Michael’s suggestion is working fine in our existing system.
I’ve now hit a bootstrapping problem when applying this to a new system, which results in “list index out of range” error.
We’re automating replacement of servers behind a reverse proxy from one set to another. With each transition, we must execute a command on one of the outgoing servers. Our current playbook works fine when there is an outgoing server – but in a new system, there is no outgoing server on the first run.
Following is the relevant portion of our playbook. When applied on a first run to our new system, “outgoing_version” is empty. The pattern “group_:&group2” contains no hosts, so the first task is not run. Not surprisingly, we then hit an error on the second hosts pattern because “outgoing” is not defined.
Is there a proper way to do this with Ansible? Perhaps some way to mark the second play conditional on “outgoing” being defined?
Clarification: Our playbook also includes plays for the incoming servers interleaved with plays for the outgoing servers. Hoping to avoid one playbook for only incoming servers – and another for incoming + outgoing servers (would have duplication).
In Ansible, the general rule is if something looks ungainly like this, don’t do it
I would probably consider having one playbook that includes another list of plays, and running either the top level one or the second one depending on whether you need to do the second step.