How to edit properly a configuration file of a service and make sure it is loaded in the service with Ansible Playbook?
65 观看
3回复
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
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 日回应 3
0像
43254 作者的声誉
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-handlers
command-line option, or by includingforce_handlers: True
in a play, orforce_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.)
0像
9 作者的声誉
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 日0像
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 日来自类别的问题 :
- ansible Ansible幂等MySQL安装Playbook
- ansible 如何在不指定库存但直接主机的情况下运行Ansible?
- ansible 安全地将Ansible剧本限制在一台机器上?
- ansible 如何获取ansible-playbook模块执行的日志/详细信息?
- ansible 在Ansible主机上运行命令
- ansible Ansible:同时部署在多个主机上
- ansible 如何获取Ansible主机文件中定义的当前计算机的主机名?
- ansible 为Ansible指定sudo密码
- ansible 如果未安装包,如何使Ansible执行shell脚本
- ansible 使用Ansible安装Bundler gem
- devops 克隆Eclipse上的Git存储库(传输错误)
- devops 使用SaltStack创建一个Mysql数据库
- devops 你如何阻止Ansible在主目录中创建.retry文件?
- devops Google Kubernetes存储在EC2中
- devops nginx在X-RateLimit-Remaining标头中设置limit_req的剩余计数
- devops 使用Terraform时的最佳做法
- devops IBM_DB Gem问题
- devops 使用Saltstack部署Jenkins
- devops Docker Machine Virtual Box驱动程序问题
- devops 如何将play框架和angular2结合起来?