6

We're now investigating Ansible to provision our servers. It's quite a default nginx, php-fpm & mysql setup. However, I am wondering about installation of these packages and how to make the playbook idempotent with the services running.

For nginx, we've a default nginx.conf and some files in conf.d/. For php, we've a php.ini, a php-fpm.conf, a pool in pool.d/ and some ini files in conf.d/. Is it the idea to overwrite all files on every ansible playbook call?

If all configurations are overwritten, is it OK to do a service nginx reload and service php5-fpm reload even when the server is under heavy load? For initial installations, a reload will not start the server, so I have to check the status first and based on that, switch between start and reload?

If I look for playbooks with a nginx installation, they often use handlers which will restart nginx. However, this is not graceful, so I don't really like that approach:

service: name=nginx state=restarted

In general, what's the common pattern to use ansible and provision servers with services like nginx, php-fpm and mysql without forcing a restart?

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99

2 Answers2

5

The service module can do reload with state=reloaded.

Configuration file won't be uploaded if the same version is already on the server. Thus, reload won't be triggerer if you use service: name=nginx state=restarted in a handler.

You can also use service: name=nginx enable=yes so the service starts at boot (and thus there is no need to explicitely start nginx, only reload if needed).

Adam
  • 6,539
  • 3
  • 39
  • 65
leucos
  • 17,661
  • 1
  • 44
  • 34
4

On the #ansible IRC channel I got already an answer which works. The pattern in general for apt systems is that the service is started after an install. So you can omit the start completely and only reload in cases when configs change.

The setup would then be (as example, Nginx is taken)

  1. Install Nginx
  2. Overwrite all config files
  3. If in #2 something changed, run a reload

This should be sufficient; when Nginx is not installed, steps 1,2 and 3 are executed. When Nginx is installed and the configs are OK, no reload happens. If we update the configuration, step #2 is causing a change so a reload happens.

This should be sufficient to cover all cases.

Mxx
  • 8,979
  • 4
  • 27
  • 37
Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99
  • Even though on apt systems service might auto start, you still want to explicitly say that. For example if a package is installed but the service does not automatically start on bootup. You want to use `service` module to make sure that all relevant services are in correct states. – Mxx Nov 30 '13 at 04:08