How to edit properly a configuration file of a service and make sure it is loaded in the service with Ansible Playbook?
When I'm developping Ansible Playbook, there's something I found annoying when I'm using handlers to trigger reload of configuration file. For example, if I consider editing a InfluxDB configuration (but it can be any services) :
- name: Update configuration influxdb template: src: influxdb.conf.j2 dest: "/etc/influxdb/influxdb.conf" notify: - restart influxdb
If my handler "restart influxdb" doesn't work (because I done an error in my Ansible script, or any other causes), there is no proper way to execute the handler again, because the template is writed.
Result I've go is that the target node is not in a good configuration because the service is not restarted (I have to do it manually).
How to make sure my new configuration is applied to the service in a idempotent way of think ?
ps : I edited my question to make it more clear to answer作者: Jérôme B 的来源 发布者： 2017 年 12 月 27 日
During development phase you may want to enable force-handlers option:
作者: Konstantin Suvorov 发布者: 2017 年 12 月 28 日
When a task fails on a host, handlers which were previously notified will not be run on that host. This can lead to cases where an unrelated failure can leave a host in an unexpected state. For example, a task could update a configuration file and notify a handler to restart some service. If a task later on in the same play fails, the service will not be restarted despite the configuration change.
You can change this behavior with the
--force-handlerscommand-line option, or by including
force_handlers: Truein a play, or
force_handlers = Truein ansible.cfg. When handlers are forced, they will run when notified even if a task fails on that host. (Note that certain errors could still prevent the handler from running, such as a host becoming unreachable.)
By default all handlers execute just once in play. You must set another play:
- name: Play1 tasks: ... handlers: ... - name: Play2 tasks: ... handlers: ...
Or... you must change environment. set var force_handlers = True Or... --force-handlers作者: Петр Иглаев 发布者: 2018 年 1 月 6 日
After several months practicing Ansible, I've got the answer of my interrogation.
It's an Ansible designing purpose habit to take. When I design a new role that aim to set-up a new service, I have two things to do and not forget :
- Flush the handlers though the yaml command
- meta: flush_handlers
- After flush, testing the service is up, for example on a mysql service
- name: Start and enable the service
- name: Ensure mysql port is started
If my configuration is wrong, my handler will be executed before the service execution test (thanks to the flush_handlers), so the service test will fail, and I can fix my mistake.作者: Jérôme B 发布者: 2018 年 5 月 18 日