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 选项参数名称用 ${} 符号引用

相关推荐
红红谈说6 分钟前
定时发布关机就停,本地定时任务的补跑策略怎么设计?
运维·服务器·定时任务·本地调度·补跑策略·幂等互斥·执行记录
宁波阿成15 分钟前
CentOS 7 环境下 Tomcat 8.5 升级至 9.0.123 操作总结
linux·centos·tomcat
4SAPI26 分钟前
企业级大模型工作流基础设施选型指南:4SAPI、硅基流动与 OpenRouter 的架构权衡
运维·人工智能·架构·自动化
幽默小吴28 分钟前
nginx篇
运维·nginx
爱和冰阔落34 分钟前
【Linux网络】UDP 服务器怎么写?从 socket、bind 到 recvfrom/sendto,手写完整回显服务器
linux·运维·服务器·网络
maochongsandai11037 分钟前
Centos上运行docker应用命令汇总
linux·docker·centos
知已难寻43 分钟前
00 · OpenStack 总纲——从云计算到 OpenStack 全景
运维·服务器·云原生·云计算·负载均衡·openstack·perl
一只QAQ1 小时前
python语法学习
linux·python·学习
七夜zippoe1 小时前
Agent 网关设计:统一入口、智能路由与负载均衡
运维·ai·log4j·负载均衡·agent·网关设计
by209991 小时前
Linux 软件安装与 Vim 编辑
linux·经验分享·vim