dependency modeling

I have a use case for Ansible where I have several code bases (say, C_A and C_B), and several services (S_A1, S_A2, S_B1, S_B2). Here S_A1 and S_A2 are essentially different executables from the same repo containing C_A. It’s relatively easy to model the dependency such that when a node is running a service, the dependent code base is deployed.

What I need is to write a somewhat generic playbook to upgrade a code base (say C_A) on all or a group of nodes. And if a node is running a corresponding service (say S_A1), the service must be stopped first, perform upgrade, and restart the service.

Ideally, I want to be able to do something like: “ansible-play -i nodes upgrade.yml -e code_base=C_A --limit group-test”

I’ve thought about modeling C_* and S_* as roles, and use role dependency to link them. But I am not sure if we can iterate through all the nodes that take a particular role.
Another way might be to define two levels of groups, a group of nodes that runs a service, and a group of services that should run a code base. In this case, at least we might be able to write a playbook like:

  • hosts: “group_{{ code_base }}”
    roles:
  • { role: role_code_base, name: “{{ code_base }}”}

But in this case, I am not sure how to trigger the right services to reload, and stop them all in the first place.

Any ideas? Thanks…

Playbooks are supposed to be a record of history, thus you really shouldn’t parameterize the name of a group.

List out the groups and store them in version control, and if neccessary, keep seperate playbooks for different groups if you like.

You can pull multiple playbooks together in one file, like so:

multi.yml:

  • include: pb_one.yml
  • include: pb_two.yml

Thanks..

But I am still curious.. is there a way to list all the roles a node is taking when running a task?

I can potentially add a line in a common config file for each role. But this won't remove a role from that file if I update the playbook or role dependency such that a node is no longer taking a role.

Any ideas?