Git+Gitlab+Ansible剧本一键部署动态页面(二)

本文介绍如何使用Gitlab创建项目,通过Ansible的roles自动化部署WordPress,包括需求说明、环境配置、剧本编写等步骤,实现一键部署动态页面。
摘要由CSDN通过智能技术生成

前言

从上篇博客《Git+Gitlab+Ansible剧本实现Nginx静态页面部署(一)》我们部署一键部署了静态页面,接下来我们实现以下一键部署动态页面吧!

项目需求

需求一:使用gitlab创建项目
需求二:使用ansible的roles实现一键化部署wordpress
每次部署需要备份之前的网站数据
使用roles
使用templates
脚本对网站监控检测
需求三: 完成之后项目上传至gitlab

项目部署环境

centos7
Gitlab服务器: 10.220.5.42
Ansible服务器: 10.220.5.41
wordpress服务器1: 10.220.5.45
防火墙以及selinux关闭状态

创建gitlab项目

使用gitlab创建一个项目
相信大家对这个已经很熟悉了,所以我就不再详细演示该过程
第一步:打开浏览器创建项目
这里我创建了一个wordpress1的项目
在这里插入图片描述

上传安装包到远程仓库

上传wordpress到gitlab
第一步:创建目录

[root@ansible ~]# mkdir /yy
[root@ansible ~]# cd /yy
[root@ansible yy]# 

第二步:找到远程仓库的位置
在这里插入图片描述第三步:下载远程仓库项目

[root@ansible yy]# git clone http://10.220.5.42/root/wordpress1.git
Cloning into 'wordpress1'...
warning: You appear to have cloned an empty repository.

第四步:上传本地安装包至远程目录

  [root@ansible yy]# cp -a wordpress/* ./wordpress1/
  [root@ansible yy]#  ls wordpress1
  [root@ansible wordpress1]# cd wordpress1
  [root@ansible wordpress1]# git add
  [root@ansible wordpress1]#   git add --all
  [root@ansible wordpress1]# git commit -m "v2.0"

第五步:web端查看
在这里插入图片描述

编写ansible剧本

上一篇博客已经详细讲解了有关roles的使用,这里我们依然是使用roles来完成该项目
第一步:创建相关目录
在ansible服务器端操作

[root@ansible ~]# mkdir /project/roles/wordpress1/{vars,tasks,files,templates} -pv

第二步:编写templates模版

[root@ansible ~]# cp /etc/httpd/conf/httpd.conf /project/roles/wordpress1/templates/httpd.conf.j2
[root@ansible~]# grep -v -E '^#|^$| +#' /project/roles/wordpress1/templates/httpd.conf.j2
  ServerRoot "/etc/httpd"
  Listen {{ port }}    #定义成变量
  Include conf.modules.d/*.conf
  User {{ user }}       #定义成变量
  Group apache
  ServerAdmin root@localhost
  ServerName {{ ansible_eth0.ipv4.address }}  #引用内置变量
  DocumentRoot {{ root }}               #定义成变量

第三步:编辑hosts配置文件

[root@ansible project]# vim /etc/ansible/hosts
[devser]
10.220.5.45

第四步:编写测试文件

[root@ansible project]# vim roles/wordpress1/files/index.php
<?php
phpinfo();
?>

第五步:编写网站健康监测脚本

[root@ansible project]# cd  roles/wordpress1/files/
[root@ansible files]# vim check.sh
[root@ansible files]# cat check.sh
#!/bin/bash
URL=$1
PORT=$2
curl -I http://$1:$2/index.php | grep "200 OK" &>/dev/null
if [ $? -eq 0 ];then
    echo "$1 status is ok"
else
    echo "$1 status is not ok"
fi

第六步:编写tasks下的文件

 [root@ansible wordpress1]# vim tasks/main.yaml
    ######## 安装 httpd php mysql ###############
    - name: install httpd
      yum: name=httpd state=present
    - name: install mysql
      yum: name=mariadb-server state=present
    - name: install php
      shell: yum install php php-mysql -y
    ######## 配置httpd #########################
    - name: make configrantion file
      template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    - name: install test page
      copy: src=index.php dest={{ root }}
    ######## 启动服务 ####################
    - name: start httpd
      service: name=httpd state=restarted
    - name: start mysql
      service: name=mariadb state=restarted
    ######### 检查部署结果是否正常 ##########
    - name: make health check
      shell: sh roles/wordpress/files/check.sh {{ ansible_eth0.ipv4.address }} {{ port }}
      delegate_to: localhost
      register: health_status
    - debug: msg="{{ health_status.stdout }}"
    ########## 从gitlab拉取代码 #################
    - name: backup old files
      shell: mv {{ root }} {{ backup_to }}
    - name: close ssl authtication
      shell: git config --global http.sslVerify false
    - name: git clone wordpress from gitlab
      git: "repo=http://{{ gitlab_user }}:{{ gitlab_pass }}@10.220.5.137/webg1/wordpress.git dest={{ root }} version=master"

第七步:编写vars下的文件

[root@ansible wordpress1]# cat /project/roles/wordpress1/vars/main.yaml
port: 82
user: apache
root: /var/www
gitlab_user: root
gitlab_pass: 12345678

第八步:编辑剧本
剧本一定要和roles在同一个目录之中
执行剧本的时候也要在roles同级目录下执行

[root@ansible wordpress1]# cat /project/wordpress1.yaml
- hosts: all
  vars:
     backup_to: "{{ root }}_{{ ansible_date_time.epoch }}"
  roles:
   - wordpress1

第九步:一键部署wordpress

未完待续… …

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值