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

相关推荐
正经教主11 分钟前
【docker基础】第九周:Docker安全与镜像优化
运维·docker·容器
tianyuanwo20 分钟前
深入解析 RISC-V 虚拟化中的 UEFI 固件配置:从 XML 到 NVRAM 的生命周期管理
xml·linux·risc-v
道川贤林41 分钟前
OrangePi 系统启动优先级修改
linux·linux驱动·orangepi·u-boot
时佃鹏1 小时前
银河麒麟 V10 重装打印服务 (CUPS)+ 打印机驱动完整教程
运维·银河麒麟系统
醉颜凉1 小时前
Elasticsearch高性能优化:Bulk API大规模数据导入性能调优全攻略
elasticsearch·性能优化·jenkins
xsc-xyc1 小时前
用 Tailscale + Syncthing 实现手机、电脑与 NAS 的跨网络文件同步
linux·网络·网络安全·智能手机·电脑
Shepherd06191 小时前
【IT 运维】Apache 使用 mod_remoteip 恢复 Cloudflare 后的真实访客 IP
运维·tcp/ip·apache
IsJunJianXin1 小时前
pdd小程序 cdp 保存响应体
linux·服务器·小程序·pdd小程序·拼多多响应体解密·小程序cdp·拼多多rpc取响应体
王二端茶倒水2 小时前
智慧园区网络运营:认证、分权、运维和安全闭环
运维·物联网·架构
爱就是恒久忍耐2 小时前
现代CMake的build方式
linux·运维·服务器