Jenkins-Ansible 插件相关用法

1. ansible-hoc 单条命令的实现

调用 shell 模块,构建步骤填写如下

运行任务,查看执行结果是否正确返回 webservers 主机的 hostname
inventory 部分也可以直接选择 Inline content 选项填写文件内容


调用 file 模块,构建步骤填写如下

运行任务,在 webservers 主机上查看 test.log 文件是否生成

2. ansible-playbook yml文件的实现

获取目标主机的的 ip,并将返回的结果存储在 result 变量中

root@jenkins \~#vim /data/jenkins/test.yml

  • hosts: webservers

remote_user: root

tasks:

  • name: excute cmd

shell:

cmd: hostname -I

register: result

  • name: show result

debug:

msg: "{{ result }}"
构建步骤填写如下

3. 实现多环境部署

yml 文件如下

root@jenkins ansible#vim /data/jenkins/test.yml

  • hosts: webservers

remote_user: root

tasks:

  • name: excute cmd

shell:

cmd: hostname -I

register: result

  • name: show result

debug:

msg: "{{ result }}"
hosts 文件如下

root@jenkins ansible#vim test-hosts

webservers

10.0.0.202 ansible_ssh_user=root

10.0.0.203 ansible_ssh_user=root

root@jenkins ansible#vim product-hosts

webservers

10.0.0.205 ansible_ssh_user=root

10.0.0.206 ansible_ssh_user=root
选择参数,定义变量名称,并添加不同环境 hosts 选项


构建步骤填写如下,host list 使用选项名称使用 $ 符号引用变量

4. 实现 ansible-playbook 参数化

yml 文件如下

root@jenkins ansible#vim /data/jenkins/test.yml

  • hosts: "{{ ansible_hosts }} "

remote_user: root

tasks:

  • name: excute cmd

shell:

cmd: hostname -I

register: result

  • name: show result

debug:

msg: "{{ result }}"

hosts 文件如下

root@jenkins ansible#vim hosts-test

webservers

10.0.0.202 ansible_ssh_user=root

appservers

10.0.0.203 ansible_ssh_user=root

root@jenkins ansible#vim hosts-product

webservers

10.0.0.205 ansible_ssh_user=root

appservers

10.0.0.206 ansible_ssh_user=root

添加第一个选项参数


添加第二个选项参数


构建步骤填写如下


高级选项填写如下,key 是 ansible host 文件中的 hosts 变量,value 选项参数名称用 ${} 符号引用

相关推荐
迪康软件zz4 分钟前
审计事件的时间线还原:事件 ID、去重键与索引选择
运维·安全·自动化运维
我命由我1234512 分钟前
Android 广播 - Android 开机广播实现开机自启应用
android·java·linux·java-ee·kotlin·android studio·android-studio
Linux界最忧郁之人15 分钟前
K8s访问控制实战|认证Authentication、鉴权Authorization、准入控制手把手理解与实操
运维·云原生·容器·rpc·kubernetes
凤希AI伴侣22 分钟前
AI 自动化实战:装好只是进门,换习惯才是难关
运维·人工智能·自动化
心如鉄补1 小时前
GitHub Actions 自动化:5 个真实运维提效案例
运维·自动化·github
沫璃染墨1 小时前
从零入门计算机网络系列(三十一):Linux I/O 多路复用详解——select 工作原理与底层机制
linux·运维·服务器·网络·c++·网络协议·信息与通信
沫璃染墨1 小时前
从零入门计算机网络系列(三十二):Linux I/O 多路复用详解——poll 工作原理、TCP 实战与性能瓶颈
linux·运维·服务器·网络·c++·网络协议·网络安全
是专家不是砖家1 小时前
pthread_mutex_t 踩内存引起的总线错误问题
linux·内存对齐·pthread_mutex_t·1字节对齐
Android系统攻城狮2 小时前
FFmpeg深度解析之av_frame_get_buffer:如何为AVFrame分配缓冲区?(八十四)
linux·ffmpeg·音视频·车载音频·ffmpeg音视频进阶
大袁同学2 小时前
【Linux网络编程】:破局孤岛通四海,纵贯五层渡风云
linux·网络·c++·后端