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

相关推荐
hj28625113 分钟前
Linux学习方法论 + 系统安全加固与性能优化 完整版笔记(含案例)
运维
刘某的Cloud23 分钟前
硬链接 和 软链接 区别
运维·系统·硬链接·软链接
jiayong2326 分钟前
harness 与 hermes-agent 扩展性、安全与运维
运维·人工智能·安全·ai·架构·智能体·harness
mounter62531 分钟前
破局与守正:eBPF 在 Linux 内存管理中的应用、演进与重构构想
linux·服务器·网络·mmu·ebpf·linux kernel
STDD41 分钟前
Linux Namespace:容器隔离的底层原理,PID、网络、挂载隔离实战
linux·运维·网络
todoitbo42 分钟前
一台 2C2G 服务器上的 KingbaseES 安装记录
运维·服务器·数据库·国产数据库
Demon1_Coder1 小时前
智能体的自定义工具
java·linux·前端
gf13211111 小时前
【精确查找python脚本是否在运行】
linux·前端·python
Sunny Boy 0011 小时前
linux环境编译Pro*C 源文件(.pc文件)
linux·c语言·oracle
Gong-Yu1 小时前
MySQL数据库运维(1)
运维·数据库·mysql·慢查询