[Jenkins + Ansible + Gitlab automated deployment Musketeers] study notes - Chapter 5-1 ~ 5-5 Freestyle Job combat

I. Introduction The Three Musketeers environment (Jenkins, Ansible, Gitlab)

Here Insert Picture Description
Delivery of a flow chart shown above, we need to prepare three cloud host (you can also use virtual machines to create three virtual machines).
The three hosts are required to prepare the installation environment and IP are as follows.
Here Insert Picture Description

Second, the Three Musketeers built environment (Jenkins, Ansible, Gitlab)

2.1, ansible environment and ssh-free under the verification Jenkisns close Login

# 登录到jenkins主机(203)
ssh [email protected]
# 切换到deploy用户
su - deploy
# 加载Python3.6的环境
source /home/deploy/.py3-a2.5-env/bin/activate
# 在Python3.6环境中加载ansible
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
# 测试ansible-playbook是否可用
ansible-playbook --version
# 测试是否可以通过远程登录到目标主机(testbox)
ssh [email protected]

Here Insert Picture Description

2.2, write nginx_playbooks file

In the repo file in the previous folder windows machine, previously prepared by ansible part test_playbooks into, and then open a command window git brash;

# 拷贝一份test_playbooks并将文件夹重命名为nginx_playbooks
cp -a test_playbooks nginx_playbooks

Here Insert Picture Description

2.2.1, enter nginx_playbooks folder file written deploy.yml

Here Insert Picture Description
Modified to the FIG.
Here Insert Picture Description

2.2.2, create dev and prod file

The testenv file, copy, and rename dev, prod
Here Insert Picture Description

2.2.3, written prod file

In the FIG., In [Nginx] dns plurality of records can be added, corresponding to multiple hosts
Here Insert Picture Description

2.2.4, written dev file

Here Insert Picture Description

2.2.5, file modifications roles / nginx / files

Testbox change the name of the folder is nginx
Here Insert Picture Description
deleted files folder foo.sh script file
and a script file used to create health_check.sh health check website
Here Insert Picture Description

#!/bin/sh

# 将传入的变量赋值给URL
URL=$1

curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check"

Here Insert Picture Description
Create an index.html file

# 写一个文本语句到index.html文件中
echo "This is my first website" > index.html

Here Insert Picture Description

2.2.6 modify files in the roles / nginx / templates

Switch to templates folder
Here Insert Picture Description
using vim nginx.conf.j2 open the file, can be seen as shown below.
Here Insert Picture Description

# For more information on configuration, see:
user              {{ user }};
worker_processes  {{ worker_processes }};

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    worker_connections  {{ max_open_file }};
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    #include /etc/nginx/conf.d/*.conf;
 	server {
        listen       {{ port }} default_server;
        server_name  {{ server_name }};

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   {{ root }};
            index  index.html index.htm;
        }

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

    }

}

2.3, write wordpress_playbooks file

Copy nginx_playbooks to wordpress_playbooks
Here Insert Picture Description

2.3.1, the main entrance to file written deploy.yml

- hosts: "wordpress"
  gather_facts: true
  remote_user: root
  roles:
    - wordpress

Here Insert Picture Description

2.3.2, the files in the preparation of inventory

Into the inventory directory
Here Insert Picture Description

2.3.2.1, written dev file

[wordpress]
test.example.com

[wordpress:vars]
server_name=test.example.com
port=8080
user=deploy
worker_processes=2
max_open_file=30000
root=/data/www
gitlab_user='root'
gitlab_pass='nis123456'

2.3.2.1, written prod file

# 将dev文件复制到prod文件中
cp -rf dev prod

You can see the contents of the above and consistent dev file
Here Insert Picture Description
and then add a gitlab account and password

gitlab_user='root'
gitlab_pass='nis123456'

Complete file as follows
Here Insert Picture Description

Third, to achieve static pages written playbook remote deployment

Nginx_playbook into the folder / nginx_playbooks / roles / nginx / tasks Road King. There is a main.yml (written before the time of testing) under the Road King's file, modify the file as follows

- name: Disable system firewall
  service: name=firewalld state=stopped

- name: Disable SELINUX
  selinux: state=disabled

- name: setup nginx yum source
  yum: pkg=epel-release state=latest

- name: write then nginx config file
  template: src=roles/nginx/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 index.html to remote
  copy: 'remote_src=no src=roles/nginx/files/index.html dest=/www/index.html mode=0755'

- name: restart nginx service
  service: name=nginx state=restarted

- name: run the health check locally
  shell: "sh roles/nginx/files/health_check.sh {{ server_name }}"
  delegate_to: localhost
  register: health_status

- debug: msg="{{ health_status.stdout }}"

Fourth, the playbook deployment scripts submitted to Gitlab

As shown in the directory structure of files before editing playbook follows.
Here Insert Picture Description
Git Bash command in the directory on the window position as shown to
Here Insert Picture Description
create a ansible-playbook-repo items on Gitlab. Then write the playbook above ansible submitted to the following tips git git statement.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Five, Freestyle task of building automation and deployment

5.1, adding a free style of nginx-freestyle-job tasks

Here Insert Picture Description

5.2, add a description

Here Insert Picture Description

5.3, adding Git

Copy address git repository Here Insert Picture Description
copied git added to the configuration in FIG.
Here Insert Picture Description

5.4, ​​add the parameter

Here Insert Picture Description

5.5, add building

#!/bin/sh

set +x
source /home/deploy/.py3-a2.5-env/bin/activate
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q

cd $WORKSPACE/nginx_playbooks
ansible --version
ansible-playbook --version

ansible-playbook -i inventory/$deploy_env ./deploy.yml -e project=nginx -e barnch=$barnch -e env=$deploy_env

Here Insert Picture Description

5.6 test build

Here Insert Picture Description
Enter the console output
Here Insert Picture Description
Here Insert Picture Description
from the output log on the map can be seen in this building success!
View windows host hosts file, add the following DNS records.
Here Insert Picture Description
Open a browser using: test.example.com URL access
Here Insert Picture Description
point, freestyle demo deployment scripts to build instances of success!

Published 76 original articles · won praise 16 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38657051/article/details/101061005