说明:只有copy模块执行成功后,才会去调用下面的handlers相关的操作。也就是说如果passwd和aaa.txt内容是一样的,并不会去执行handlers里面的shell相关命令。这种比较适合配置文件发生更改后,重启服务的操作。
执行:ansible-playbook /etc/ansinle/handlers.yml
11.ansible实例-安装Nginx
思路:先在一台机器上编译安装好Nginx、打包,然后再用ansible去下发
进入ansible配置文件目录:cd /etc/ansible
创建一个nginx_install目录,方便管理:mkdir nginx_install
进入该目录:cd nginx_install
mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
说明:roles目录下有两个角色,commen为一些准备操作,install为安装nginx的操作。每个角色下面又有几个目录,handlers下面是当发生改变时要执行的操作,通常用在配置文件发生改变,重启服务。files为安装时用到的一些文件,meta为说明信息,说明角色依赖等信息,tasks里面是核心的配置文件,templates通常存一些配置文件,启动脚本等模块文件,vars下为定义的变量。
详细步骤:
1.编译安装Nginx可以参考LNMP部分
2.安装完Nginx后可以看到安装目录下的内容:ls /usr/local/nginx/
3.切换目录:cd /usr/local/
4.打包压缩Nginx安装目录:tar czf nginx.tar.gz nginx
5.切换到ansible配置文件目录:cd /etc/ansible
6.创建nginx_install目录:mkdir nginx_install
7.进入该目录:cd nginx_install
8.创建子目录:mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
9.将相关文件拷贝到指定目录下:
cp /usr/local/nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
10.编写common/tasks的总入口配置文件:
vim /etc/ansible/nginx_install/roles/common/tasks/main.yml
- name: Install initialization require software
yum: name=https://www.linuxidc.com/Linux/2017-03/{{ item }} state=installed
with_items:
- zlib-devel
- pcre-devel
- openssl-devel
11.编写install/vars的总入口配置文件:
vim /etc/ansible/nginx_install/roles/install/vars/main.yml
nginx_user: www
nginx_basedir: /usr/local/nginx
12.编写install/tasks的copy配置文件:
vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml
- name: Copy Nginx Sofrware
copy: src=https://www.linuxidc.com/nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software
shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script
template: src=https://www.linuxidc.com/Linux/2017-03/nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config
template: src=https://www.linuxidc.com/Linux/2017-03/nginx.conf dest=https://www.linuxidc.com/Linux/2017-03/{{ nginx_basedir }}/conf/ owner=root group=root mode=0644
13.编写install/tasks的install配置文件:
vim /etc/ansible/nginx_install/roles/install/tasks/install.yml
- name: Create Nginx User
user: name=https://www.linuxidc.com/Linux/2017-03/{{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service
service: name=https://www.linuxidc.com/Linux/2017-03/nginx state=started
- name: Add Boot Start Nginx Service
shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz
14.编写install/tasks的总入口配置文件:
vim /etc/ansible/nginx_install/roles/install/tasks/main.yml
- include: copy.yml
- include: install.yml
15.编写整个程序的总入口配置文件:
vim /etc/ansible/nginx_install/install.yml
---
- hosts: 192.168.147.138
remote_user: root
gather_facts: True
roles:
- common
- install
16.执行:ansible-playbook /etc/ansible/nginx_install/install.yml
客户端:
查看上述三个包是否已经安装成功:rpm -qa | egrep 'pcre|openssl|zlib'
查看Nginx是否安装成功:ls /usr/local/nginx/
查看服务是否启动成功:ps aux | grep nginx
查看加入服务列表是否成功:chkconfig --list nginx
11.ansible实例-管理Nginx配置文件
生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面我们来写个管理Nginx配置文件的playbook
mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启Nginx服务的命令