Trigger ansible handler when a variable is changed
You should be able to watch the file for changes using incrond. For example (from the linked documenation)
_You need to run program 'abc' with the full file path as an argument every time a file is changed in /var/mail. One of the solutions follows:
/var/mail IN_CLOSE_WRITE abc $@/$#
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 module
Like this:
- name: Set innodb-log-file-size for MySQL.
lineinfile:
dest: /etc/mysql/my.cnf
line: 'innodb-log-file-size = {{ innodb_log_file_size }}'
notify: restart mysql
You would then need to create a handler for each action you listed. The task above would only return CHANGED
and trigger the configured handlers
when the value of innodb_log_file_size
changed.
I assuming here that you are using template
module to create the mysql configuration. template
module returns CHANGED
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
and lineinfile
module because in subsequent Ansible runs both task would always return CHANGED
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 via template
, check again via command module and notify if value changed.
- name: Register innodb-log-file-size in my.cnf
command: grep -Fxq "innodb-log-file-size" /etc/mysql/my.cnf
register: innodb_log_file_size_pre
always_run: True
ignore_errors: True
changed_when: False
- name: Create my.cnf via template.
template:
src: my.cnf.j2
dest: /etc/mysql/my.cnf
- name: Restart mysql service when innodb_log_file_size changed
debug: msg="Restart mysql to activate innodb_log_file_size change"
when: innodb_log_file_size_pre !== innodb_log_file_size
notify: restart mysql