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 }}" 构建步骤填写如下 ![](https://i-blog.csdnimg.cn/direct/780d6ba85ce64177a4b1b975bed51739.png)

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

相关推荐
超龄超能程序猿23 分钟前
Docker GPU插件(NVIDIA Container Toolkit)安装
运维·docker·容器
Xの哲學1 小时前
Linux SMP 实现机制深度剖析
linux·服务器·网络·算法·边缘计算
2501_906150561 小时前
私有部署问卷系统操作实战记录-DWSurvey
java·运维·服务器·spring·开源
知识分享小能手1 小时前
Ubuntu入门学习教程,从入门到精通,Ubuntu 22.04的Linux网络配置(14)
linux·学习·ubuntu
皇族崛起1 小时前
【视觉多模态】- scannet 数据的 Ubuntu 百度网盘全速下载
linux·ubuntu·3d建模·dubbo
岳来1 小时前
docker 从 Path 值看容器启动命令
运维·docker·容器
CAU界编程小白2 小时前
Linux系统编程系列之进程控制(下)
linux·进程控制
RisunJan2 小时前
Linux命令-ifconfig命令(配置和显示网络接口的信息)
linux·运维·服务器
LaoWaiHang2 小时前
Linux基础知识04:pwd命令与cd命令
linux
杭州泽沃电子科技有限公司2 小时前
面对风霜雨雪雷电:看在线监测如何为架空线路筑牢安全网
运维·人工智能·在线监测·智能监测