注意:首先准备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 root@192.168.14.5

测试免密结果

[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 .ssh]$ ssh root@192.168.14.5
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 root@192.168.14.6
/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.
root@192.168.14.6's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.14.6'"
and check to make sure that only the key(s) you wanted were added.

[tomcat@RZ-DEV-COM-XZSX-JENKINS-01 ~]$ ssh root@192.168.14.6
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 多模块和单模块的发布

 

版权声明:本文为blueskyli原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/blueskyli/p/12213232.html