How to edit properly a configuration file of a service and make sure it is loaded in the service with Ansible Playbook?

ansible devops

65 观看


280 作者的声誉

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
    src: influxdb.conf.j2
    dest: "/etc/influxdb/influxdb.conf"
    - 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 日

回应 3


43254 作者的声誉

During development phase you may want to enable force-handlers option:

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-handlers command-line option, or by including force_handlers: True in a play, or force_handlers = True in 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.)

作者: Konstantin Suvorov 发布者: 2017 年 12 月 28 日


9 作者的声誉

By default all handlers execute just once in play. You must set another play:

- name: Play1
- name: Play2

Or... you must change environment. set var force_handlers = True Or... --force-handlers

作者: Петр Иглаев 发布者: 2018 年 1 月 6 日


280 作者的声誉


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 service: name: mysql state: started enabled: yes - name: Ensure mysql port is started wait_for: port: 3306 delay: 3 timeout: 300

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 日