ansible-playbook在公司程序发布的使用案例

2021-09-17  本文已影响0人  天草二十六_简村人

一. 入口程序

control.sh user-service install 通过调用ansible执行发布命令

#!/bin/bash

host_path=/pre/xxphp/bin/hosts
app_path=/var/lib/awx/projects/php_pre_deploy/app.yaml
run(){
    service=$1
    option=$2
    host=$3
    limit="$4"
    ansible-playbook -e "APPNAME=$service" -t $option $app_path -i $host $4
}

case ${2,,} in

install|rollback)
    [ $# -eq 4 ] && [ "$3" == "--limit" ] && {
        run $1 $2 $host_path "$3 $4"
    } || run $1 $2 $host_path
;;
*)
    echo "请输入参数(1.服务名 2.执行项 [3.limit 4.ip] )3~4为可选项须一起使用。"
;;
esac

1. hosts

在playbook中的每一个play都可以选择在哪些服务器和以什么用户完成,hosts一行可以是一个主机组、主机、多个主机,中间以冒号分隔,可使用通配模式。

/pre/xxphp/bin/hosts内容:

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

[user-service]
172.16.9.63

[pay-service]
172.16.7.63

2.app.yaml

/var/lib/awx/projects/php_pre_deploy/app.yaml 的内容是:

# 第一步,在本机执行版本校验对比的脚本任务
- hosts: "localhost"   #定义需要执行主机
  gather_facts: false
  vars:
    - APP_PATH: /pre/xxphp
    - APP_PKG_UPLOAD: /pre/php
    - AWX_APP_USER: phppreuser
    - RUN_APP_USER: nginx
    - DIFF_VERSION_SCRIPT_PATH: /pre/xxphp/bin/check_version.sh
  roles:
    - localapp

# 第二步,在远程机器执行
- hosts: "{{ APPNAME }}"  #定义需要执行主机
  remote_user: xxadmin    #指定远程主机执行的用户名
  become: True
  become_method: sudo
  gather_facts: false
  vars:
    - APP_PATH: /pre/xxphp
    - APP_PKG_UPLOAD: /pre/php
    - AWX_APP_USER: phppreuser
    - RUN_APP_USER: nginx
  serial: 1
  roles:
    - app

二.版本对比脚本

#! /bin/bash
APPNAME=$1

new_version=$(cat /pre/php/$APPNAME/.version | awk -F '[.-]' -v OFS=" " '{print $1,$2,$3}') 
current_version=$(cat /pre/xxphp/$APPNAME/packages/$APPNAME/.version  2> /dev/null | awk -F '[.-]' -v OFS=" " '{print $1,$2,$3}' )

version1=($new_version)
version2=($current_version)

if [ "${#version1[@]}" -lt "3" ]; then
   echo "no"
   exit 0
fi

if [ "${#version2[@]}" -eq "0" ]; then
   echo "yes"
   exit 0
fi


if  test ${version1[0]} -gt ${version2[0]} ; then
    echo "yes"
    exit 0
elif test ${version1[0]} -eq ${version2[0]} ; then
   if test ${version1[1]} -gt ${version2[1]};then
       echo "yes"
       exit 0
   elif test ${version1[1]} -eq ${version2[1]};then
        if test ${version1[2]} -gt ${version2[2]};then
            echo "yes"
            exit 0
        fi
   fi
fi

echo "no"

三.grant_ssh.sh

ssh免密操作

[ "$USER" != "phppreuser" ] &&{
    echo 请用phppreuser操作
    exit 1
}
ip=$1
ssh-copy-id -i ~/.ssh/id_rsa.pub xxadmin@${ip}
上一篇下一篇

猜你喜欢

热点阅读