# 官方提供@yunTaoScripts Handlers && jinja2 🔥🔥
- 实现配置文件变化了才触发操作,无变化则不触发操作。
# Handlers
[root@server1 chap8]# cat handlers.yaml
---
- hosts: xyt
vars:
http_port: 8001
tasks:
- name: install httpd
yum: name=httpd state=installed
- name: copy config file
template:
src: ./httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart httpd2 # 如果此部分重新执行就会触发notify。
- name: start firewalld
firewalld:
port: "{{http_port}}/tcp"
permanent: yes
immediate: yes
state: enabled
- name: start httpd
service: name=httpd state=started
handlers: # 最后执行,不是触发的时候就执行。
- name: restart httpd1
service: name=httpd state=restarted
- name: restart httpd2
service: name=httpd state=restarted
- 查看模版文件
[root@server1 chap8]# head -n 5 httpd.conf.j2
ServerRoot "/etc/httpd"
Listen {{http_port}}
ServerName {{ansible_fqdn}}:{{http_port}}
Include conf.modules.d/*.conf
User apache
# jinja2
是个模版语言,具体使用方法可以参考下http://doc.yonyoucloud.com/doc/jinja2-docs-cn/index.html (opens new window)
← 快速链接