zoukankan      html  css  js  c++  java
  • ansible 拷贝文件并重启服务

    Ansible 安装 

    只需要在ansible 服务器上安装
    yum install -y epel-release
    yum install -y ansible
     
     
    服务器生成密钥对
    ssh-keygen -t rsa  直接回车即可,不用设置密钥密码
     
    把公钥(id_rsa.pub)内容放到对方客户端机器的/root/.ssh/authorized_keys里面
     
     
    Ansible更改配置文件
     
    vi  /etc/ansible/hosts  //增加

    [adservers]
    172.20.0.70

    客户端 建立一个脚本

    [root@adserver4 sh]# cat /data/sh/adserver_update.sh
    #!/bin/bash


    pidno=`ps aux|grep adserver-beta|grep -v "grep"|awk '{print $2}'`

    kill -9 $pidno

    if [ $? -ne 0 ]; then
    echo "old adserver kill failed"
    exit 1
    fi

    sleep 5

    adserverpid=`ps aux|grep adserver-beta|grep -v "grep"|awk '{print $2}'`

    if [ "$adserverpid" ];then
    echo "new version adserver is running now"

    fi

    建立另外一个脚本

    [root@adserver4 sh]# cat /data/sh/packetanal_update.sh
    #!/bin/bash


    pidno=`ps aux|grep packetAnal|grep -v "grep"|awk '{print $2}'`

    kill -9 $pidno

    if [ $? -ne 0 ]; then
    echo "old packetAnal kill failed"
    exit 1
    fi

    sleep 5

    packetpid=`ps aux|grep packetAnal|grep -v "grep"|awk '{print $2}'`

    if [ "$packetpid" ];then
    echo "new version packetAnal is running now"

    fi

    配置可执行权限

    chmod +x /data/sh/*.sh

    服务器上配置 playbook 的yml文件

    [root@test-server28 ansible]# cat /etc/ansible/adserver_update.yml
    ---
    - name: handlers adserver
    hosts: adservers
    user: root
    tasks:
    - name: copy file
    copy: src=/etc/ansible/adserver-beta dest=/data/website/ owner=root group=root mode=0755
    notify: adserver handlers
    handlers:
    - name: adserver handlers
    shell: /bin/bash /data/sh/adserver_update.sh

     

    [root@test-server28 ansible]# cat /etc/ansible/packetanal_update.yml
    ---
    - name: handlers adserver
    hosts: adservers
    user: root
    tasks:
    - name: copy file
    copy: src=/etc/ansible/packetAnal dest=/data/website/ owner=root group=root mode=0755
    notify: packetAnal handlers
    handlers:
    - name: packetAnal handlers
    shell: /bin/bash /data/sh/packetanal_update.sh

    在客户端上删除 adserver, packetanal文件 

    执行ansible 分发进程

    [root@test-server28 ansible]# ansible-playbook /etc/ansible/packetanal_update.yml

    PLAY [handlers adserver] **************************************************************************************************************************************************

    TASK [Gathering Facts] ****************************************************************************************************************************************************
    ok: [172.20.0.70]

    TASK [copy file] **********************************************************************************************************************************************************
    changed: [172.20.0.70]

    RUNNING HANDLER [packetAnal handlers] *************************************************************************************************************************************
    changed: [172.20.0.70]

    PLAY RECAP ****************************************************************************************************************************************************************
    172.20.0.70 : ok=3 changed=2 unreachable=0 failed=0

     显示进程文件分发成功, 进程重新启动

    [root@test-server28 ansible]# ansible-playbook /etc/ansible/adserver_update.yml

    PLAY [handlers adserver] **************************************************************************************************************************************************

    TASK [Gathering Facts] ****************************************************************************************************************************************************
    ok: [172.20.0.70]

    TASK [copy file] **********************************************************************************************************************************************************
    changed: [172.20.0.70]

    RUNNING HANDLER [adserver handlers] ***************************************************************************************************************************************
    changed: [172.20.0.70]

    PLAY RECAP ****************************************************************************************************************************************************************
    172.20.0.70 : ok=3 changed=2 unreachable=0 failed=0

     
  • 相关阅读:
    kafka 简单调试
    audit调优
    html使用的特殊符号< &gt: &等 意义对照
    nohup /dev/null 2>&1 含义详解
    K8S etcd参数优化
    Linux下安装VMware Tools 的方法
    PHP操作MySQL数据库5个步骤
    Win7下mysql root账户登录提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)解决方案
    VS 报cmath(19): error C2061: 语法错误: 标识符“acosf” 错误
    win7 装了VB虚拟机 开始挺好用 后来突然就打不开了 提示如下错误:(如图)创建 COM 对象失败.
  • 原文地址:https://www.cnblogs.com/weifeng1463/p/8303587.html
Copyright © 2011-2022 走看看