I would like to trigger a handler when a variable is changed.
For example I have in mysql config file the innodb-log-file-size config item. I would like to do a couple of actions when this item is changed:
- ensure MySQL is running (we must start from a stable state)
- run in mysql: SET GLOBAL innodb_fast_shutdown = 0
- stop MySQL
- move /var/lib/mysql/ib_logfile[01] to a backup folder
- start MySQL
- check that MySQL is running fine by running a MySQL query
See also: https://dba.stackexchange.com/a/1265/3574
The only problem I have is how can I determine that a variable (actually some specific text) was changed in the configuration file.
I am interested in a generic approach how to solve this problem. For my particular case I have in mind a couple of solutions.
Edit 1: I am using template module.
You should be able to watch the file for changes using incrond. For example (from the linked documenation)
There incrontab(5) man page is also useful and contains further examples.
This will only be able to tell you that the file has changed (close_write) it won't be able to tell you what was changed. To find out what was changed I think you're going to need to write some scripts.
For a pure Ansible solution you could set the
innodb-log-file-size
with lineinfile moduleLike this:
You would then need to create a handler for each action you listed. The task above would only return
CHANGED
and trigger the configuredhandlers
when the value ofinnodb_log_file_size
changed.I assuming here that you are using
template
module to create the mysql configuration.template
module returnsCHANGED
when any parameter set via Ansible in the config file changed.lineinfile
module enables you to trigger handlers for a specific change.This strategy has however the bad side effect that you can't mix
template
andlineinfile
module because in subsequent Ansible runs both task would always returnCHANGED
and therefor break idempotence of the play.edit
After thinking a bit about the problem I would recommend the following strategy: check via
command
module, create viatemplate
, check again via command module and notify if value changed.Based on the help received from the other answers I decided to move the option in a separate file and trigger the restart+log_expansion when this file is changed. Bellow are the details.
In /etc/my.cnf file, I added:
In roles/mysql/tasks/main.yml I added:
roles/mysql/tasks/expand-innodb-log-file.yml:
roles/mysql/templates/set_innodb_log_file.cnf.j2: