Centos 7.2 Jenkins+Ansible+Gitlab 基礎配置
注意:首先準備jenkins伺服器 如何搭建jenkins
由於上篇文章中jenkins是採用war並部署在tomcat中來完成的安裝,所以這裡隆重介紹下啟動tomcat的使用者:tomcat,下面會用到,文章中啟動jar和tomcat都是使用的tomcat使用者,注意jenkins和目標伺服器的使用者要一致,不然ansible推送和呼叫jar的時候可能會出現意想不到的問題;詳細的內容會在下一節搭建job來說明。
安裝ansible
python 我就使用系統自帶版本了
也可自行升級python版本
本文不是專注介紹ansible的,雖然ansible網上安裝方式有很多,但這裡就是用最簡單的方式安裝了
yum -y install ansible
安裝完成後 輸入如下命令檢視安裝情況
[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 xzsx-micro-parent]$ python --version Python 2.7.5 [tomcat@RZ-DEV-COM-XZSX-JENKINS-01 xzsx-micro-parent]$ ansible --version ansible 2.4.2.0 config file = /etc/ansible/ansible.cfg configured module search path = [u'/home/tomcat/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /bin/ansible python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
配置hosts /etc/ansible/hosts
[msa_server] 192.168.14.5 [task_server] 192.168.14.6
注: 這裡我用2臺節點來分別演示ansible模組推送成功和不成功來做下對比
配置ssh
在ansible主機的tomcat使用者下,執行
ssh-keygen -t rsa
[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ansible]$ cd /home/tomcat/.ssh/ [tomcat@RZ-DEV-COM-XZSX-JENKINS-01 .ssh]$ ll total 8 -rw------- 1 tomcat tomcat 1675 Jan 17 23:47 id_rsa -rw-r--r-- 1 tomcat tomcat 415 Jan 17 23:47 id_rsa.pub
可以看到我們的祕鑰對已經生成了,然後將公鑰拷貝到192.168.14.5中
ssh-copy-id -i [email protected]
測試免密結果
[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 .ssh]$ ssh [email protected] Warning: Permanently added '192.168.14.5' (ECDSA) to the list of known hosts. Last login: Sun Jan 19 15:17:56 2020 from 192.168.14.2 [root@RZ-DEV-APP-XZSX-MSA-01 ~]# [root@RZ-DEV-APP-XZSX-MSA-01 ~]# cd /root/.ssh/ [root@RZ-DEV-APP-XZSX-MSA-01 .ssh]# ll total 4 -rw------- 1 root root 415 Jan 19 11:21 authorized_keys [root@RZ-DEV-APP-XZSX-MSA-01 .ssh]# cat authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzbdGnAUp9CeySymKNeo6mlfBDijsXM3ptP0P+uO/3XUMlZiFujsACb05KThZ+Q8JpYRNNijnL72RHoBix+E618w2TqCtAQ9pzxnO/dgcCgoZVWc9nP8HQIkr+mOEKQHfK8yQZlQ0q+3XEr3vRcjNWKzMt7yPSj8l/F9MWBUtGYGffwVDNfv8p3Y4SRHaL4/ExFmZ7kGtmIqEb6TWeEJhLwXtodOlqXpV3uJAHzHelRDKiecJCIwi2kj/BhmAIU1KKAk/MrLjCYTkLI1udhNxfnxc5RELfwUQ+rnXHkSa661p7ysOSSIj+wdkJUL719vVu8EZhYQw+ddSAHrX6c/31 tomcat@RZ-DEV-COM-XZSX-JENKINS-01 [root@RZ-DEV-APP-XZSX-MSA-01 .ssh]#
測試我們ansible的hosts檔案,注意,由於是在tomcat使用者下生成的ssh祕鑰,所以需要在tomcat使用者下執行如下命令
[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ~]$ ansible all -m ping 192.168.14.6 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.14.6' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).\r\n", "unreachable": true }192.168.14.5 | SUCCESS => { "changed": false, "ping": "pong" }
可以看到由於192.168.14.5這臺機器我們配置了tomcat使用者下ssh祕鑰,所以結果是success,而192.168.14.6沒有配置,因此報錯了,好了下面讓我們在14.6這臺伺服器上也配置下ssh
[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ~]$ ssh-copy-id -i [email protected] /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Warning: Permanently added '192.168.14.6' (ECDSA) to the list of known hosts. [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]'" and check to make sure that only the key(s) you wanted were added. [tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ~]$ ssh [email protected] Warning: Permanently added '192.168.14.6' (ECDSA) to the list of known hosts. Last failed login: Sun Jan 19 15:32:22 CST 2020 from 192.168.14.2 on ssh:notty There were 2 failed login attempts since the last successful login. Last login: Fri Jan 17 20:44:08 2020 from 192.168.14.2 [root@RZ-DEV-APP-XZSX-TASK-01 ~]# cat /root/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzbdGnAUp9CeySymKNeo6mlfBDijsXM3ptP0P+uO/3XUMlZiFujsACb05KThZ+Q8JpYRNNijnL72RHoBix+E618w2TqCtAQ9pzxnO/dgcCgoZVWc9nP8HQIkr+mOEKQHfK8yQZlQ0q+3XEr3vRcjNWKzMt7yPSj8l/F9MWBUtGYGffwVDNfv8p3Y4SRHaL4/ExFmZ7kGtmIqEb6TWeEJhLwXtodOlqXpV3uJAHzHelRDKiecJCIwi2kj/BhmAIU1KKAk/MrLjCYTkLI1udhNxfnxc5RELfwUQ+rnXHkSa661p7ysOSSIj+wdkJUL719vVu8EZhYQw+ddSAHrX6c/31 tomcat@RZ-DEV-COM-XZSX-JENKINS-01 [root@RZ-DEV-APP-XZSX-TASK-01 ~]#
[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ~]$ ansible all -m ping 192.168.14.6 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.14.5 | SUCCESS => { "changed": false, "ping": "pong" } [tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ~]$
安裝git
我們的jenkins需要從git中下載專案,所以需要在jenkins伺服器中安裝git
yum -y install git
[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ~]$ git --version git version 1.8.3.1
jenkins配置git
安裝jenkins外掛
Maven Integration Hidden Parameter Active Choices Ansible 以上四個外掛就夠了
等待安裝完畢即可
jenkins配置全域性資訊
以上的jdk,maven,ansible 需要按著自己伺服器上的路徑配置即可,到此為止我們的jenkins基礎配置就完成了,我將會在下一章中來交大家新建 java 多模組和單模組的釋出