1.cd /etc/ansible;mkdir nginx_config
2.cd nginx_config;mkdir roles;cd roles;
3.mkdir old new;cd new/
4.mkdir vars files tasks handlers
5.将配置文件拷贝到对应的 files下
cp /usr/local/nginx/conf/nginx.conf files/
cp -r /usr/local/nginx/conf/vhosts files/
6.定义变量
# vim vars/main.yml
nginx_basedir: /usr/local/nginx
7.定义 handlers
# vim handlers/main.yml
- name: restart nginx
shell: /etc/init.d/nginx reload
8.定义 tasks
# vim tasks/main.yml
- name: copy conf file
copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644
with_items:
- { src: nginx.conf, dest: conf/nginx.conf }
- { src: vhosts, dest: conf/ }
notify: restart nginx
9.定义总的入口文件
# vim update.yml
---
- hosts: 192.168.32.105
user: root
roles:
- new
10.vi /etc/ansible/nginx_config/roles/new/files/vhosts/1.conf
# sahdusah
11.vim
/etc/ansible/nginx_config/roles/new/files/nginx.conf
include vhosts/*.conf
11.发布
ansible-playbook update.yml
回滚
rsync -av new/files/ old/files/
# vim backup.yml
---
- hosts: 192.168.32.105
user: root
roles:
- old
ansible-playbook backup.yml
实例:
git clone git://github.com/dl528888/ansible-examples.git