Chapter 5-1 ~ 5-5 Freestyle Job to combat
- I. Introduction The Three Musketeers environment (Jenkins, Ansible, Gitlab)
- Second, the Three Musketeers built environment (Jenkins, Ansible, Gitlab)
- 2.1, ansible environment and ssh-free under the verification Jenkisns close Login
- 2.2, write nginx_playbooks file
- 2.2.1, enter nginx_playbooks folder file written deploy.yml
- 2.2.2, create dev and prod file
- 2.2.3, written prod file
- 2.2.4, written dev file
- 2.2.5, file modifications roles / nginx / files
- 2.2.6 modify files in the roles / nginx / templates
- 2.3, write wordpress_playbooks file
- Third, to achieve static pages written playbook remote deployment
- Fourth, the playbook deployment scripts submitted to Gitlab
- Five, Freestyle task of building automation and deployment
I. Introduction The Three Musketeers environment (Jenkins, Ansible, Gitlab)
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.
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]
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
2.2.1, enter nginx_playbooks folder file written deploy.yml
Modified to the FIG.
2.2.2, create dev and prod file
The testenv file, copy, and rename dev, prod
2.2.3, written prod file
In the FIG., In [Nginx] dns plurality of records can be added, corresponding to multiple hosts
2.2.4, written dev file
2.2.5, file modifications roles / nginx / files
Testbox change the name of the folder is nginx
deleted files folder foo.sh script file
and a script file used to create health_check.sh health check website
#!/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"
Create an index.html file
# 写一个文本语句到index.html文件中
echo "This is my first website" > index.html
2.2.6 modify files in the roles / nginx / templates
Switch to templates folder
using vim nginx.conf.j2 open the file, can be seen as shown below.
# 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
2.3.1, the main entrance to file written deploy.yml
- hosts: "wordpress"
gather_facts: true
remote_user: root
roles:
- wordpress
2.3.2, the files in the preparation of inventory
Into the inventory directory
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
and then add a gitlab account and password
gitlab_user='root'
gitlab_pass='nis123456'
Complete file as follows
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.
Git Bash command in the directory on the window position as shown to
create a ansible-playbook-repo items on Gitlab. Then write the playbook above ansible submitted to the following tips git git statement.
Five, Freestyle task of building automation and deployment
5.1, adding a free style of nginx-freestyle-job tasks
5.2, add a description
5.3, adding Git
Copy address git repository
copied git added to the configuration in FIG.
5.4, add the parameter
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
5.6 test build
Enter the console output
from the output log on the map can be seen in this building success!
View windows host hosts file, add the following DNS records.
Open a browser using: test.example.com URL access
point, freestyle demo deployment scripts to build instances of success!