63-CICD持续集成工具-Jenkins结合Ansible实现自动化批量部署
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了63-CICD持续集成工具-Jenkins结合Ansible实现自动化批量部署相关的知识,希望对你有一定的参考价值。
集成 Ansible 的任务构建
- 安装 Ansible 环境
#包安装即可(新版ubuntu包安装Ansible会缺少配置文件,可copy旧版的部分)
[root@jenkins ~]#apt install ansible -y
[root@jenkins ~]#ansible --version
ansible 2.10.8
config file = None -->可以看到这里缺少配置文件,本次实验使用内容较简单我们手动创建
configured module search path = [/root/.ansible/plugins/modules, /usr/share/ansible/plugins/modules]
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]
#创建ansible配置文件
[root@jenkins ansible]#vim ansible.cfg
[root@jenkins ansible]#cat ansible.cfg
[defaults]
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp #本机的临时命令执行目录
#forks = 5 #默认并发数
#sudo_user = root #默认sudo 用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#remote_port = 22
#host_key_checking = False #检查对应服务器的host_key,建议取消此行注释,实现第一次连接自动信任目标主机
#log_path=/var/log/ansible.log #日志文件,建议启用
#module_name = command #默认模块,可以修改为shell模块
[privilege_escalation] #普通用户提权配置
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
[root@jenkins ansible]#ansible --version
ansible 2.10.8
config file = /etc/ansible/ansible.cfg
configured module search path = [/root/.ansible/plugins/modules, /usr/share/ansible/plugins/modules]
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]
- 创建主机清单文件
[root@jenkins ~]#cd /etc/ansible/
[root@jenkins ansible]#vim hosts-test
[root@jenkins ansible]#vim hosts-product
[root@jenkins ansible]#cat hosts-test ;cat hosts-product
[webservers]
192.168.11.202
[appservers]
192.168.11.203
[webservers]
192.168.11.204
[appservers]
192.168.11.205
- 打通Ansible主机到被控制端机器的key验证
[root@jenkins ansible]#ssh-copy-id 192.168.11.202
[root@jenkins ansible]#ssh-copy-id 192.168.11.203
[root@jenkins ansible]#ssh-copy-id 192.168.11.204
[root@jenkins ansible]#ssh-copy-id 192.168.11.205
- Jenkins更新镜像源到国内网站
[root@jenkins ~]#sed -i.bak s#updates.jenkins.io/download#mirror.tuna.tsinghua.edu.cn/jenkins#g /var/lib/jenkins/updates/default.json
[root@jenkins ~]#sed -i s#www.google.com#www.baidu.com#g /var/lib/jenkins/updates/default.json
- 安装 Ansible 插件
安装插件后,添加了ansible的构建步骤
- 准备 Playbook文件
[root@jenkins script]#vim hostname.yml
[root@jenkins script]#cat hostname.yml
- hosts: " ansible_hosts "
remote_user: root
tasks:
- name: excute cmd
shell:
cmd: hostname -I
register: result
- name: show result
debug:
msg: " result "
- 创建 Ansible Playbook 的任务
- 创建参数化任务 - 使构建可以有面向测试和生产两种选项
- 添加第二个参数选项 - Jenkins参数传入到Playbook中使用
#Jenkins参数用作playbook中的参数逻辑
选菜单 --> hosts_list (jenkins变量)--> ansible_hosts (ansible 变量) --> playbook
注意:设置参数名要与playbook中的变量对应
[root@jenkins ~]#cat /data/script/hostname.yml
- hosts: " ansible_hosts "
remote_user: root
tasks:
- name: excute cmd
shell:
cmd: hostname -I
register: result
- name: show result
debug:
msg: " result "
[root@jenkins ~]#cat /etc/ansible/hosts-product
[webservers]
192.168.11.204
[appservers]
192.168.11.205
- 添加构建任务脚本,并添加参数构建选项
- 点"高级"添加ansible的变量,添加Ansible Playbook的变量注
注意: 此处的Extra Variables 指的是ansible的变量
key为ansible Playbook的变量名
Value可以是固定值,或者是任务中的变量,注意:需要用$变量名形式
如下配置相当于: ansible-playbook -e "ansible_hosts=$ansible_hosts"
- 保存构建设置并执行
再次测试生产服务器的app服务是否执行playbook
Jenkins控制台输出
Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/Ansible-Playbook-hostname
[Ansible-Playbook-hostname] $ ansible-playbook /data/script/hostname.yml -i /etc/ansible/hosts-product -f 5 -e ansible_hosts=appservers
PLAY [appservers] **************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.11.205]
TASK [excute cmd] **************************************************************
changed: [192.168.11.205]
TASK [show result] *************************************************************
ok: [192.168.11.205] =>
"msg":
"changed": true,
"cmd": "hostname -I",
"delta": "0:00:00.015849",
"end": "2023-02-18 12:07:10.440169",
"failed": false,
"rc": 0,
"start": "2023-02-18 12:07:10.424320",
"stderr": "",
"stderr_lines": [],
"stdout": "192.168.11.205 240e:368:9f27:c701:5054:ff:fe88:763f ",
"stdout_lines": [
"192.168.11.205 240e:368:9f27:c701:5054:ff:fe88:763f "
]
PLAY RECAP *********************************************************************
192.168.11.205 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finished: SUCCESS
我是moore,大家一起加油!!!
以上是关于63-CICD持续集成工具-Jenkins结合Ansible实现自动化批量部署的主要内容,如果未能解决你的问题,请参考以下文章