playbook-《when和handlers》

环境准备

服务器系统Centos7.3
内存1G
CPU2核
IP地址10.0.0.41
服务器系统Centos7.3
内存1G
CPU2核
IP地址10.0.0.43
服务器系统Centos7.3
内存1G
CPU2核
IP地址10.0.0.44

在41Ansible管理主机上操作

vim when.yml

在这里插入图片描述

ansible-playbook when.yml 
ansible all -m shell -a 'ls /opt/'

在这里插入图片描述

在43主机上操作,修改主机名

hostname node0-43
exit
ssh root@10.0.0.43

在44主机上操作,修改主机名

hostname node0-44
exit
ssh root@10.0.0.44

在41Ansible管理主机上操作

vim nginx.yml

在这里插入图片描述

---
- hosts: webservers
  tasks:
     - name: "推送nginx源码包"
       unarchive: src=nginx-1.12.0.tar.gz dest=/usr/local/src/
     - name: "安装编译环境"
       yum: name=gcc,gcc-c++,pcre-devel,zlib-devel state=latest
     - name: "安装nginx"
       shell: cd /usr/local/src/nginx-1.12.0/ && ./configure && make && make install
     - name: "推送配置文件"
       copy: src=nginx.conf  dest=/usr/local/nginx/conf/nginx.conf
       notify: reload-nginx
       tags: config-nginx
     - name: "启动nginx"
       shell: ss -ntl | grep -qw 80 || /usr/local/nginx/sbin/nginx
       tags: start-nginx
     - name: "修改测试页面"
       shell: echo "<h1>hello world!<h1>" > /usr/local/nginx/html/index.html
     - name: "关闭防火墙"
       service: name=firewalld state=stopped
     - name: "关闭selinux"
       shell: setenforce 0

  handlers:
     - name: "reload-nginx"
       shell: ss -ntlp | grep -q nginx && /usr/local/nginx/sbin/nginx -s reload
ansible-playbook -t config-nginx nginx.yml 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值