配置ansible實現無密鑰交互
<code>安裝阿里雲YUM源碼[root@node1 ~]# cat <>/etc/yum.repos.d/epel.repo[epel]name=epel for aliyunbaseurl=https://mirrors.aliyun.com/epel/7/x86_64/enabled=1gpgcheck=0[os]name=os for aliyunbaseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/enabled=1gpgcheck=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.72192.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: class="lazy" src="//p2.ttnews.xyz/loading.gif" data-original=/home/jenkins/.ssh/id_rsa.pub dest=/home/jenkins/.ssh/authorized_keys owner=jenkins group=jenkins mode=600 /<code>
配置ansible支持無密鑰登錄
<code>[ops@node1 ~]$ egrep "^[^#\\[]" /etc/ansible/ansible.cfginventory = /etc/ansible/hostsremote_tmp = ~/.ansible/tmplocal_tmp = ~/.ansible/tmpforks = 5poll_interval = 15sudo_user = rootexecutable = /bin/bashprivate_key_file = /home/jenkins/.ssh/id_rsaconnect_timeout = 30connect_retries = 30connect_interval = 1 /<code>
切換至jenkins用戶測試無密鑰登錄是否正常
配置我們的node2作為我們的web前端
<code>[root@node2 ~]# yum install mysql mysql-devel httpd httpd-devel php mariadb-server php-mysql配置測試頁面查看我們的PHP環境是否安裝成功[root@node2 ~]# cat /var/www/html/index.php[root@node2 ~]# systemctl start httpd[root@node2 ~]# systemctl start mariadb/<code>
<code>[root@node2 ~]# yum install mysql mysql-devel httpd httpd-devel php mariadb-server php-mysql配置測試頁面查看我們的PHP環境是否安裝成功[root@node2 ~]# cat /var/www/html/index.php[root@node2 ~]# systemctl start httpd[root@node2 ~]# systemctl start mariadb/<code>
配置我們的node3作為備份服務器
<code>[root@node3 ~]# mkdir -pv /data/www/sourcecode/ [root@node3 ~]# chown -R jenkins:jenkins /data/www//<code>
配置我們的jenkins
<code>首先安裝以下插件:Environment Injector PluginSafe Restart PluginJavadoc PluginjQuery pluginExtended Choice Parameter Plug-InPre SCM BuildStep PluginRobot Framework pluginGit Parameter Plug-In/<code>
創建一個自由風格的軟件項目
配置參數化構建
配置GIT參數
配置構建後的操作
配置環境
<code>[jenkins@node1 ~]$ sudo mkdir /data/www/sourcecode/ #此處為代碼存放目錄[jenkins@node1 ~]$ sudo chown -R jenkins:jenkins /data/www/ #將屬主修改為jenkins以免出現權限問題/<code>
配置分發腳本
<code>[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 10finame=$1datetime=$2destination='jenkins'##此處的destination是72和73,73作為備份服務器所以也需要推送,72則需要部署上線操作ansible ${destination} -m copy -a "class="lazy" data-original=/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被殺掉起不起來的情況/<code>
配置我們的72和73
<code>配置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>
正式發佈我們的代碼
查看node2部署情況
查看node3備份情況
閱讀更多 動漫資深愛好者 的文章