安装nginx:
[root@cent7_yzil ansible]# cat install_nginx.yml
---
# install nginx
- hosts: webservers
remote_user: root
gather_facts: no
force_handlers: yes #无论task中的任何一个task失败,仍强制调用handlers
tasks:
- name: add group nginx
group: name=nginx state=present
- name: add user nginx
user: name=nginx state=present group=nginx
- name: install nginx
yum: name=nginx state=present
- name: config file
copy: src=files/nginx.conf dest=/etc/nginx/nginx.conf
tags:conf #设置便签
notify: restart nginx service
- name: web page
copy: src=files/index.html dest=/usr/share/nginx/html/index.html
- name: start nginx
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx service
service: name=nginx state=restarted
[root@cent7_yzil ansible]#
[root@cent7_yzil ~]# yum install nginx
[root@cent7_yzil ~]# rpm -ql nginx
/etc/nginx/nginx.conf #nginx配置文件所在的路径
[root@cent7_yzil ansible]# cp /etc/nginx/nginx.conf files/
[root@cent7_yzil ansible]# vi files/index.html
[root@cent7_yzil ansible]# cat files/index.html
<h1>
hello world!
</h1>
[root@cent7_yzil ansible]#
卸载nginx:
[root@cent7_yzil ansible]# cat remove_nginx.yml
---
# remove nginx
- hosts: webservers
gather_facts: no
tasks:
- name: stop nginx
service: name=nginx state=stopped enabled=no
- name: remove nginx
yum: name=nginx state=absent
- name: remove user nginx
user: name=nginx state=absent
- name: remove group nginx
group: name=nginx state=absent
- name: config file
file: path=files/nginx.conf state=absent
- name: web page
file: path=/usr/share/nginx/html/index.html state=absent
[root@cent7_yzil ansible]#