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?
Use systemctl restart ansible-tower to restart services on clustered environments instead. Also you must restart each cluster node for certain changes to persist as opposed to a single node for a localhost install.
Just run the task service: name=httpd state=started with the option --check . This tells you, if the service needs to be started, which means that it is down. If the task shows no change, it is up already.
How to stop a service. Set the name parameter to the service name and the state parameter to stopped to stop a service. If the service is not running, Ansible will do nothing.
string. added in 2.2 of ansible.builtin. The service module actually uses system specific modules, normally through auto detection, this setting can force a specific module. Normally it uses the value of the 'ansible_service_mgr' fact and falls back to the old 'service' module when none matching is found.
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).
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With