1. ansible-handlers在变更执行操作
  1) 编写playbook的handlers的配置文件

 1 [root@test-1 bin]# vim /ansible/nginx/bin/handlers.yaml
2 [root@test-1 bin]# cat /ansible/nginx/bin/handlers.yaml
3 ---
4 - hosts: web1
5 remote_user: root
6 gather_facts: no
7
8 tasks:
9 - name: Copy nginx config file
10 copy:
11 src: /ansible/nginx/conf/site.conf
12 dest: /etc/nginx/conf.d/site.conf
13 notify:
14 - reload nginx #这里是指定要执行那个的handlers,对应下面的name,我们可以定义多个handlers,通过handlers name来识别不同的handlers
15
16 handlers: #这里表示如果拷贝文件成功,就使用service服务重启nginx
17 - name: reload nginx
18 service:
19 name: nginx
20 state: reloaded

2) 查看原来服务器的nginx配置文件

 1 [root@test-1 bin]# ansible web1 -a "cat /etc/nginx/conf.d/site.conf"
2
3 192.168.200.133 | CHANGED | rc=0 >>
4 server {
5 listen 80;
6 server_name www.ctnrs.com;
7 location / {
8 root /var/www/html;
9 index index.html;
10 }
11 }
12
13 192.168.200.132 | CHANGED | rc=0 >>
14 server {
15 listen 80;
16 server_name www.ctnrs.com;
17 location / {
18 root /var/www/html;
19 index index.html;
20 }
21 }

3) 修改nginx配置文件

 1 [root@test-1 bin]# vim /ansible/nginx/conf/site.conf
2 [root@test-1 bin]# cat /ansible/nginx/conf/site.conf
3 server {
4 listen 80;
5 server_name test.scajy.cn;
6 location / {
7 root /var/www/html;
8 index index.html;
9 }
10 }

2. 验证handlers文件是否正确
  1) 验证编写handlers文件

1 [root@test-1 bin]# ansible-playbook  --syntax-check  handlers.yaml
2
3
4 playbook: handlers.yaml

3. 执行handlers文件
  1) 执行文件

 1 [root@test-1 bin]# ansible-playbook    handlers.yaml
2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
3
4
5 PLAY [web1] ******************************************************************************************************************************************************************************************************************************************************************
6
7 TASK [Copy nginx config file] ************************************************************************************************************************************************************************************************************************************************
8 changed: [192.168.200.133]
9 changed: [192.168.200.132]
10
11 RUNNING HANDLER [reload nginx] ***********************************************************************************************************************************************************************************************************************************************
12 changed: [192.168.200.132]
13 changed: [192.168.200.133]
14
15 PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
16 192.168.200.132 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
17 192.168.200.133 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

4. 验证是否成功执行了
  1)验证文件是否改变

 1 [root@test-1 bin]# ansible web1 -a "cat /etc/nginx/conf.d/site.conf"
2
3
4 192.168.200.133 | CHANGED | rc=0 >>
5 server {
6 listen 80;
7 server_name test.scajy.cn;
8 location / {
9 root /var/www/html;
10 index index.html;
11 }
12 }
13
14 192.168.200.132 | CHANGED | rc=0 >>
15 server {
16 listen 80;
17 server_name test.scajy.cn;
18 location / {
19 root /var/www/html;
20 index index.html;
21 }
22 }

2) 验证nginx是否正常运行

 1 [root@test-1 bin]# ansible web1 -m shell -a "netstat -lntup|grep 80"
2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
3
4 192.168.200.133 | CHANGED | rc=0 >>
5 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17881/nginx: master
6
7 192.168.200.132 | CHANGED | rc=0 >>
8 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17792/nginx: master
9
10 [root@test-1 bin]# ansible web1 -m shell -a "ps -ef |grep nginx"
11 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
12
13 192.168.200.132 | CHANGED | rc=0 >>
14 root 17792 1 0 15:04 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
15 nginx 18524 17792 0 20:41 ? 00:00:00 nginx: worker process
16 root 18735 18730 58 20:51 pts/1 00:00:00 /bin/sh -c ps -ef |grep nginx
17 root 18737 18735 0 20:51 pts/1 00:00:00 grep nginx
18
19 192.168.200.133 | CHANGED | rc=0 >>
20 root 17881 1 0 15:04 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
21 nginx 18613 17881 0 20:41 ? 00:00:00 nginx: worker process
22 root 18825 18820 62 20:51 pts/1 00:00:00 /bin/sh -c ps -ef |grep nginx
23 root 18827 18825 0 20:51 pts/1 00:00:00 grep nginx

3) curl模拟浏览器请求

1 [root@test-1 bin]# curl 192.168.200.133 -H "host:test.scajy.cn"
2 hello Ansible
3 [root@test-1 bin]# curl 192.168.200.132 -H "host:test.scajy.cn"
4 hello Ansible

ansible-handlers变更执行操作的更多相关文章

  1. C# 对包含文件或目录路径信息的 System.String 实例执行操作

    在字符串操作中有一类比较特殊的操作,就是对包含文件或目录路径信息的 System.String 实例执行操作.比如根据一个表示路径的字符串获取其代表的文件名称.文件夹路径.文件扩展名等.在很多时候,我 ...

  2. [转]js中confirm实现执行操作前弹出确认框的方法

    原文地址:http://www.jb51.net/article/56986.htm 本文实例讲述了js中confirm实现执行操作前弹出确认框的方法.分享给大家供大家参考.具体实现方法如下: 现在在 ...

  3. 利用Fiddler修改请求信息通过Web API执行操作(Action)实例

    本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复261或者20170724可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...

  4. php定时执行操作及ob_flush()与flush()的使用

    版权声明:本文为博主原创文章,未经博主允许不得转载. http://blog.csdn.net/qq_38125058 一: 每隔30s执行一次,将字符串写入文件 // 30秒执行一次 ignore_ ...

  5. dotnetcore ef 调用多个数据库时用户命令执行操作报错

    dotnetcore ef 调用多个数据库时用户命令执行操作报错 1.多个DbContext 时报错: 报错: More than one DbContext was found. Specify w ...

  6. 多个iframe中根据src获取特定iframe并执行操作

    多个iframe中根据src获取特定iframe并执行操作 前言:在项目中做一个批量编辑工单时需要在一大堆的iframe中的某一个iframe里边再用模态框的形式显示编辑区域,然后再在模态框里边加入i ...

  7. loadrunner 脚本开发-执行操作系统命令

    脚本开发-执行操作系统命令 by:授客 QQ:1033553122 思路: 用loadrunner system()函数 函数原型: int system( const char *string ); ...

  8. SQL0668N 不允许对表"xxx"执行操作,原因码为 "1"

    使用db2 load导入30万条记录到某个表,成功后发现表被锁了,并显示: SQL0668N  不允许对表"xxx"执行操作,原因码为 "1" google了一 ...

  9. JDBC连接数据库及其执行操作

    作者:Alvin 功能:数据库连接与实现增删改查 时间:2019年3月4日08点33分 参考文章:https://www.2cto.com/database/201805/743741.html 一. ...

随机推荐

  1. Ajxax技术-1

    1.什么是Ajax Ajax: Asynchronous javascript and xml (异步javascript和xml). ==Ajax并不是一种新技术,而是已有技术的集合.JavaScr ...

  2. Linux下安装Sublime Text 3 及使用快捷方式

    1.添加sublime text3的仓库 首先按下快捷键ctrl+alt+t打开终端: 在终端输入:sudo add-apt-repository ppa:webupd8team/sublime-te ...

  3. PDF启动增加字段

    ALTER TABLE `cnoa_system_fs` ADD `reviewStatus` INT(1) NOT NULL DEFAULT '0' AFTER `isArchive`;ALTER  ...

  4. meta生成器 —— 表单元素组件

    手写代码? meta(json)需要手写吗?别闹,手写多麻烦呀,我这么懒怎么可能手写,这辈子都别想,所以要弄个工具出来,咱们说干就干. 这个工具,说白了本身就是一个表单,一个meta属性对应一个met ...

  5. jmeter做简单的压测

    一.JMeter概述jmeter除了可以做借口测试外,还可以做压力测试:首先介绍jmeter中各个组件在压力测试中扮演的角色 1)线程(Threads(Users))即虚拟用户,线程组里可设置需要模拟 ...

  6. 登录SQL Server服务器时的服务器名称

    在本地访问Microsoft SQL Server 2008服务器时,首先就是要填写正确的服务器名称.经测试,该名称可以有多种写法,具体如下: 写法一: X\sqlexpress(X即计算机名) 写法 ...

  7. 深入理解Java之装箱与拆箱

    一.Java数据类型 1.在说装箱与拆箱之前,先说一下Java的基本数据类型,Java从数据类型上可以划分为值类型与引用类型,值类型是四类八种,分别是: 整数型:byte̵,short̵,int̵,l ...

  8. Dos拒绝服务攻击DNS、SNMP、NTP放大攻击和slowhttptest攻击工具(三)

    DNS放大攻击产生大流量的攻击方式 udpDNS放大效果-查询请求流量小,但响应流量可能非常巨大-dig ANY baidu.com @1.1.1.1 //向1.1.1.1的服务器查询域名解析,流量放 ...

  9. linux如何把普通用户添加到sudo组

    sudo原理:运行命令时,系统检查/etc/sudoers 配置文件,看这个用户是否有执行sudo的权限,如果有权限,系统要求输入用户自己的密码,如果密码输入正确,系统会以root身份运行 passw ...

  10. Redis 三大缓存

    Redis 三大缓存      过去的有些事情不一定要忘记,但一定要放下. 背景:Redis 三大缓存:缓存穿透.缓存击穿.缓存雪崩,是Redis 面试必须要掌握的东西. 一.缓存穿透 1.概念简述 ...