最後wordpress項目:
main.yml
[root@jenkins wordpress_playbooks]# cat roles/wordpress/tasks/main.yml
- name: Update yum dependency
shell: 'yum update -y warn=False'
- name: Disable system firewall
service: name=firewalld state=stopped
- name: Disable SELINX
selinux: state=disabled
- name: Setup epel yum source for nginx and mariadb(mysql)
yum: pkg=epel-release state=latest
- name: Setup webtatic yum source for php-fpm
yum: name=https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
- name: Ensure nginx is at the latest version
yum: pkg=nginx state=latest
- name: Ensure git is at the latest version
yum: pkg=git state=latest
- name: Write the nginx config file
template: src=roles/wordpress/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: Create nginx root folder
file: 'path={{ root }} state=directory owner={{ user }} group={{ user }} mode=0755'
- name: Copy info.php to remote
copy: 'remote_src=no src=roles/wordpress/files/info.php dest=/data/www/info.php mode=0755'
- name: Restart nginx service
service: name=nginx state=restarted
- name: Setup php-fpm
command: 'yum install -y php70w php70w-fpm php70w-common php70w-mysql php70w-gd php70w-xml php70w-mbstring php70w-mcrypt warn=False'
- name: Restart php-fpm service
service: name=php-fpm state=restarted
- name: Copy php-fpm config file to remote
copy: 'remote_src=no src=roles/wordpress/files/www.conf dest=/etc/php-fpm.d/www.conf mode=0755 owner={{ user }} group={{ user }} force=yes'
- name: Restart php-fpm service
service: name=php-fpm state=restarted
- name: Run the health check locally
shell: "sh roles/wordpress/files/health_check.sh {{ server_name }} {{ port }}"
delegate_to: localhost
register: health_status
- debug: msg="{{ health_status.stdout }}"
- name: Setup mariadb(mysql)
command: "yum install -y mariadb mariadb-server warn=False"
- name: Backup current www folder
shell: 'mv {{ root }} {{ backup_to }}'
- name: Close git ssl verification
shell: 'git config --global http.sslVerify false'
- name: Clone WordPress repo to remote
git: "repo=https://{{ gitlab_user | urlencode }}:{{ gitlab_pass | urlencode }}@gitlab.uscwifi.cn/root/wordpress.git dest=/data/www version={{ branch }}"
when: project == 'wordpress'
- name: Change www folder permission
file: "path=/data/www mode=0755 owner={{ user }} group={{ user }}"
dev清單
[root@jenkins wordpress_playbooks]# cat inventory/dev
[wordpress]
test1.uscwifi.cn
[wordpress:vars]
server_name=test1.uscwifi.cn
port=8080
user=deploy
worker_processes=2
max_open_file=30000
root=/data/www
gitlab_user='root'
gitlab_pass='qqqq....'
prod清單
[root@jenkins wordpress_playbooks]# cat inventory/prod
[wordpress]
test1.uscwifi.cn
[wordpress:vars]
server_name=test1.uscwifi.cn
port=80
user=deploy
worker_processes=4
max_open_file=65505
root=/data/www
gitlab_user='root'
gitlab_pass='qqqq....'
pipeline 腳本
#!groovy
pipeline {
agent {node {label 'master'}}
environment {
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
}
parameters {
choice(
choices: 'dev\nrprod',
description: 'Choose deploy environment',
name: 'deploy_env'
)
string (name: 'branch', defaultValue: 'master', description: 'Fill in your ansible repo branch')
}
stages {
stage ("Pull deploy code") {
steps{
sh 'git config --global http.sslVerify false'
dir ("${env.WORKSPACE}"){
git branch: 'master', credentialsId: '3aef3ca1-3587-4ede-ab6e-9144e93a2d8d', url: 'https://gitlab.uscwifi.cn/root/ansible-playbook-repo.git'
}
}
}
stage ("Check env") {
steps {
sh """
set +x
user=`whoami`
if [ $user == deploy ]
then
echo "[INFO] Current deployment user is $user"
source /home/deploy/.py3-a2.5-env/bin/activate
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
echo "[INFO] Current python version"
python --version
echo "[INFO] Current ansible version"
ansible-playbook --version
echo "[INFO] Remote system disk space"
ssh [email protected] df -h
echo "[INFO] Rmote system RAM"
ssh [email protected] free -m
else
echo "Deployment user is incorrect, please check"
fi
set -x
"""
}
}
stage ("Anisble deployment") {
steps {
input "Do you approve the deployment?"
dir("${env.WORKSPACE}/wordpress_playbooks"){
echo "[INFO] Start deployment"
sh """
set +x
source /home/deploy/.py3-a2.5-env/bin/activate
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
ansible-playbook -i inventory/$deploy_env ./deploy.yml -e project=wordpress -e branch=$branch -e env=$deploy_env
set -x
"""
echo "[INFO] Deployment finished..."
}
}
}
}
}