安装阿里云YUM源码 [root@node1 ~]# cat <<EOF>>/etc/yum.repos.d/epel.repo [epel] name=epel for aliyun baseurl=https://mirrors.aliyun.com/epel/7/x86_64/ enabled=1 gpgcheck=0 [os] name=os for aliyun baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/ enabled=1 gpgcheck=0 EOF [root@node1 ~]$ yum clean all [root@node1 ~]$ yum makecache 安装ansible [cce@node1 ~]$ yum list |grep ansible* ansible.noarch 2.3.0.0-3.el7 epel ansible-doc.noarch 2.3.0.0-3.el7 epel ansible-inventory-grapher.noarch 2.3.2-1.el7 epel ansible-lint.noarch 3.4.9-1.el7 epel ansible-openstack-modules.noarch 0-20140902git79d751a.el7 epel ansible-review.noarch 0.13.0-2.el7 epel kubernetes-ansible.noarch 0.6.0-0.1.gitd65ebd5.el7 epel python2-ansible-tower-cli.noarch 3.1.3-1.el7 epel [root@node1 ~]$ yum install -y ansible 利用jenkins密钥对实现ansible免密分发 [jenkins@node1 ~]$ ls .ssh/ id_rsa id_rsa.pub known_hosts 将ansible所有权给我们的jenkins用户 [root@node1 ~]# chown -R jenkins:jenkins /etc/ansible/ 将两台客户端服务器加入我们的ansible认真hosts表里,并归纳为test组 [jenkins@node1 ~]# tail -3 /etc/ansible/hosts [jenkins] 192.168.1.72 192.168.1.73 编写简单的YML来实现以上创建步骤(我们的jenkins用户也需要开通sudo权限来完成一些例如服务重启的工作) [root@node1 ~]# cat key.yml --- - hosts: jenkins remote_user: root tasks: - name: Create User user: name=jenkins - name: grant permissions shell: usermod -aG wheel jenkins - name: Mkdir Key Director file: path=/home/jenkins/.ssh mode=0700 owner=jenkins group=jenkins state=directory - name: Copy Public Key copy: src=/home/jenkins/.ssh/id_rsa.pub dest=/home/jenkins/.ssh/authorized_keys owner=jenkins group=jenkins mode=600
[ops@node1 ~]$ egrep "^[^#\[]" /etc/ansible/ansible.cfg inventory = /etc/ansible/hosts remote_tmp = ~/.ansible/tmp local_tmp = ~/.ansible/tmp forks = 5 poll_interval = 15 sudo_user = root executable = /bin/bash private_key_file = /home/jenkins/.ssh/id_rsa connect_timeout = 30 connect_retries = 30 connect_interval = 1
[root@node2 ~]# yum install mysql mysql-devel httpd httpd-devel php mariadb-server php-mysql 配置测试页面查看我们的PHP环境是否安装成功 [root@node2 ~]# cat /var/www/html/index.php <?php phpinfo(); ?> [root@node2 ~]# systemctl start httpd [root@node2 ~]# systemctl start mariadb
[root@node3 ~]# mkdir -pv /data/www/sourcecode/ [root@node3 ~]# chown -R jenkins:jenkins /data/www/
首先安装以下插件: Environment Injector Plugin Safe Restart Plugin Javadoc Plugin jQuery plugin Extended Choice Parameter Plug-In Pre SCM BuildStep Plugin Robot Framework plugin Git Parameter Plug-In
[jenkins@node1 ~]$ sudo mkdir /data/www/sourcecode/ #此处为代码存放目录 [jenkins@node1 ~]$ sudo chown -R jenkins:jenkins /data/www/ #将属主修改为jenkins以免出现权限问题
[jenkins@node1 ~]$ cat /usr/local/scripts/local-dev-jenkins_test-deploy.sh #!/bin/bash ### check if [ ! -n "$1" ];then echo "Please enter your file‘s name." exit 10 fi name=$1 datetime=$2 destination=‘jenkins‘ ##此处的destination是72和73,73作为备份服务器所以也需要推送,72则需要部署上线操作 ansible ${destination} -m copy -a "src=/data/www/sourcecode/${name}.tar.gz dest=/data/www/jenkins_packet/" && \ ansible 192.168.1.72 -m shell -a "sudo nohup bash -x /usr/local/scripts/local-dev-jenkins_test-deploy.sh ${name} ${datetime}" ##nohup在tomcat需要重启的时候必须使用,否则会出现tomcat被杀掉起不起来的情况
配置73的源代码存放目录 [root@node3 ~]# mkdir /data/www/jenkins_packet/ [root@node3 ~]# chown -R jenkins:jenkins /data/www/ 配置72的代码存放目录和部署脚本 [root@node2 ~]# mkdir /data/www/jenkins_packet/ [root@node2 ~]# chown -R jenkins:jenkins /data/www/ [root@node2 ~]# mkdir /usr/local/scripts/ [root@node2 scripts]# cat /usr/local/scripts/local-dev-jenkins_test-deploy.sh #!/bin/bash # name=${1}.tar.gz SITE_NAME=${1} date_time=$2 if [ -d /tmp/${SITE_NAME} ];then rm -rf /tmp/${SITE_NAME} fi mkdir /tmp/${SITE_NAME} rm -rf /var/www/html_bak #systemctl stop httpd if [ -d /var/www/html ];then mv /var/www/html /var/www/html_bak fi tar -zxf /data/www/jenkins_packet/${name} -C /tmp/${SITE_NAME} mv -f /tmp/${SITE_NAME} /var/www/html chown -R apache:apache /var/www/html #systemctl start httpd ##PHP环境无需重启
本文出自 “Char” 博客,谢绝转载!
原文地址:http://charcce.blog.51cto.com/4255555/1934111