1
votes

This might be more of a design question than a syntax question. I have a set of Ansible playbooks responsible for setting up various systemd services to configure a server a particular way. I'm sure this is a very typical usecase. However, my question is: what is the best way to shutdown and disable services before continuing with the installation and configuration? Here are my requirements:

  • Should be able to just shutdown and disable services across a set of servers without then running the installation
  • Should also be able to shutdown/disable and then proceed with the installation
  • If we are running a shutdown followed by an install, all non-dedicated hosts should be cleaned and the dedicated host group should be chosen for install

For example, let's say I have 3 servers: A, B, C and I have 2 services to install and configure: httpd and nginx. My hosts file looks like this:

[apache]
A
C

[nginx]
B

The end result should have httpd running on servers A and C (and shutdown/disabled on B), with Nginx running on B (and shutdown/disabled on A and C).

My inital thoughts on possible solutions to this are:

  1. Create separate playbooks for running each uninstall (because I already have playbooks for installation) (This might be correct, but certainly a lot of code duplication and more work)
  2. Create some additional task that sits alongside the main.yml file in the existing installation roles (Is this possible? This feels right if so.)
  3. A master uninstall playbook that can be invoked directly before running any install playbooks. (This feels wrong)
2

2 Answers

1
votes

My first though was “yes, should be closed for low quality”.

Try to remove boilerplate from your situation to make it easier to understand and probably you will find the answer.

Hint: I do not see any group that defines your managed servers, i doubt you want to use “all”.

The key is to use sub-group membership to determine what needs to be removed or installed. One playbook can do the trick.

0
votes

the dirty way to do it would be to create two install roles, one for apache and one for nginx, and create two more roles per web server, one for starting the service and one for stopping the service.

create a playbook that first targets all hosts and include the install roles, then targets [apache] with the apache-start & nginx-stop roles, then finally targets [nginx] with the nginx-start & apache-stop roles.

This is a very systematic way of doing what you want to do but to me it seems simple?