ansible tower

1 安装需求

1.1 重要路径

ansible tower 路径

cat /etc/tower/settings.py

STATIC_ROOT = '/var/lib/awx/public/static'

PROJECTS_ROOT = '/var/lib/awx/projects'

JOBOUTPUT_ROOT = '/var/lib/awx/job_status'

SECRET_KEY = open('/etc/tower/SECRET_KEY', 'rb').read().strip()

 

2.2配置文件

2.11 下载源文件配置

cat /var/lib/awx/projects/ftp-sync/sync.yml

 

---
- hosts: all
  gather_facts: no
  tasks:
   - name: Create dst dir ...
     file:
       path: "{{ dst }}"
       state: directory
   - name: sync wuhan code to beijing ...
     shell: bash /jpdata/rsync/sync-ftp.sh {{src}} {{dst}}
   - name: view code list ...
     shell:  find {{dst}}  -type f
     register: var1
   - set_fact: var2="{{var1.stdout}}"
   - debug: msg="{{var2}}"
View Code

 

2.12 源文件服务器传输配置

#!/bin/bash
if [ ! $1 ] || [ ! $2 ];then
echo "请输入参数 \$1 \$2"
echo -e "eg. \nsync-ftp.sh ftp路径(project之后的部分) 本地路径(/jpdata/fb/项目目录)"
exit
fi
rsync -rtvaz --password-file=/jpdata/rsync/rsync.password backupgp@61.183.231.142::whftpfile/projects/$1 $2
View Code

 

 

2.21 同步目标文件配置

cat /var/lib/awx/projects/deploy/deploy.yml

---
- hosts: all
  gather_facts: no
  tasks:
   - name: set date ...
     shell: date  +%Y%m%d%H%M%S
     register: var1
   - set_fact: var2="{{var1.stdout}}"
   - debug: msg="{{var2}}"  

   - name: Create temp dir ...
     file:
       path: "/jpdata/current/{{var2}}/tmp"
       state: directory

   - name: Sync code ...
     shell: /var/lib/awx/projects/deploy/deploy.sh main /jpdata/current/{{var2}} {{code_path}} {{project_path}}
     notify: Clear publication directory

   - name: Git push git ...
     shell: /var/lib/awx/projects/deploy/deploy.sh gitpush {{project_path}} {{var2}}

  handlers:
   - name: Clear publication directory
     file:
       path: "/jpdata/current/{{var2}}"
       state: absent
View Code

 

2.22 配置脚本

cat /var/lib/awx/projects/deploy/deploy.sh

#!/bin/bash
func=$1
curpath=$2
codepath=$3
propath=$4
function main() {
    filename=$(basename $2)
    extension="${filename##*.}"
    file="${filename%.*}"
        tmppath=$1"/tmp"
        projectname=$(getprojectname $3)
        gitpull $3
    if [ $extension == "gz" ]; then
        cd $1/tmp && tar xvf $2
                t=$(diffdir $tmppath $3)
                dirname=$(getdirname $tmppath)
                if  [ $t == "1" ]; then
                    if  [[ "$filename" =~ "update" ]]; then
                    echo "update..."
                    else
                        echo "new deploy..."
                           rm -rf $3/$projectname 
                    fi
                    cd $1/tmp && scp -r * $3
                else
                    if  [[ "$filename" =~ "update" ]]; then
                        echo "update..."
                    else
                        echo "new deploy..."
                        rm -rf $3/$projectname
                    fi
                    cd $1 && mv tmp/$dirname $projectname  && rm -rf tmp
                    scp -r * $3
                fi 
    elif [ $extension == "zip" ]; then
        cd $1/tmp && unzip $2
                t=$(diffdir $tmppath $3)
                dirname=$(getdirname $tmppath)
                if [ $t == "1" ]; then
                        if  [[ "$filename" =~ "update" ]]; then
                            echo "update..."
                        else
                                echo "new deploy..."
                                   rm -rf $3/$projectname
                        fi
                        cd $1/tmp && scp -r * $3
                else
                        if  [[ "$filename" =~ "update" ]]; then
                                echo "update..."
                        else
                                echo "new deploy..."
                                  rm -rf $3/$projectname
                        fi
                        cd $1 && mv tmp/$dirname $projectname && rm -rf tmp
                        scp -r * $3
                fi
    elif [ $extension == "war" ]; then
        if  [[ "$filename" =~ "update" ]]; then
                        echo "update..."
                else
                        echo "new deploy..."
            rm -rf $3/$projectname
                fi
                cd $1 && mkdir $projectname && cd $projectname  && /usr/jdk1.8.0_221/bin/jar xvf $2 && cd .. &&  scp -r * $3
    else
        echo "unknown type!"
    fi
}


function getprojectname() {
        echo $(awk '/project_name:/{print $2}' ${1}/.gitlab-ci.yml | sed 's/"//g')
}

function diffdir() {
        dirname=$(getdirname $1)
        projectname=$(getprojectname $2)
    if [ "$dirname" == "$projectname" ]; then
        echo "1"
    else
        echo "0"
    fi
}

function getdirname(){
    for file in `ls $1`
    do
        if [ -d $1"/"$file ]
        then
            echo $file
        fi
                break;
    done
}

function gitpull() {
    cd $1 && git pull
}

function gitpush() {
        cd $1 && find . -name '.gitignore' | xargs rm -rf
    cd $1 && git add --all && git commit -m "$2 ..." && git push -u origin master
}

case $1 in
 main)
        main $2 $3 $4 $5
        ;;
 gitpush)
        gitpush $2 $3
        ;;
 *)
        echo "Usage: $name [main|gitpush]"
        exit 1
        ;;
esac
exit 0
View Code

 

posted @ 2020-10-26 15:56  快闪开,我要起飞了  阅读(683)  评论(0编辑  收藏  举报