jenkins 配置 ansible

1、首先确保你的jenkins成功安装,且能够正常访问

2、安装ansible

yum -y install ansible

ansible --version

3、配置 ansible.cfg

[root@deploy ~]# vim /etc/ansible/ansible.cfg (可以不修改)
 1.长连接设置 
将 #ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s 
修改为 ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d 
2.开启pipelining 
将 #pipelining = False 
修改为 pipelining = True 
3.开启缓存 在 #gathering = implicit 下面追加
 gathering = smart fact_caching_timeout = 86400 fact_caching = jsonfile fact_caching_connection = /tmp/ansible_fact_cache

配置ansible 密钥

[root@deploy ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NRuBl+TBQow5l3nb+E/MCg95tpZRF/+amoRTaBIhTXw root@deploy
The key's randomart image is:
+---[RSA 2048]----+
|      .O+*+.     |
|      +.XoEo   . |
|       o.==+    o|
|         oo=. . o|
|        S +o.+ ..|
|         o+o= + .|
|          o*.B o |
|           o*.+  |
|           .o.   |
+----[SHA256]-----+

#将秘钥拷贝到客户机
[root@deploy ansible]# ssh-copy-id  10.241.0.2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

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

#测试ansible能否正常运行
[root@deploy ansible]# ansible  client -m ping
10.241.0.2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

4、在jenkins上新建自由风格的项目,关联好gitlab项目

5、项目构建

可以使用gitlab-ci.yml(需要你是项目的主程序员)

jenkins 配置 ansible_第1张图片

也可以使用web钩子

jenkins 配置 ansible_第2张图片

 

6、编写主控机的yml文件(测试在被控端的机器上新建文件夹)(路径:/etc/ansible/playbook/test.yml)playbook文件夹是新建的

---

- hosts: webservers
  remote_user: root
  tasks:
  - name: mkdir jenkins-test-dir
    file: path=/data/jenkins-test-dir state=directory

7、编写主控机的hosts文件(路径:/etc/ansible/hosts)
 

#主机组名称 ansible_sudo_pass 远程主机root密码
[webservers]
172.18.231.175 ansible_sudo_pass=sudo sitech1995

8、jenkins找到要构建的项目,点击立即构建,出现success就成功了

jenkins 配置 ansible_第3张图片

你可能感兴趣的:(jenkins 配置 ansible)