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

相关推荐
任风雨几秒前
15.1.2.linux常见操作用例
linux·服务器
YouEmbedded29 分钟前
解码LVGL样式
linux·lvgl·lvgl样式
lifejump1 小时前
端口隔离技术的实验与验证(eNSP)
linux·服务器·网络
zzzsde1 小时前
【Linux】基础指令(4):基础指令&&热键&&shell运行原理
linux·运维·服务器
艾莉丝努力练剑1 小时前
【Git:基本操作】深度解析Git:从初始Git到熟悉基本操作
大数据·linux·c++·人工智能·git·gitee·指令
NiKo_W2 小时前
Linux TcpSocket编程
linux·服务器·网络·udp·socket·多线程·tcp
Eternal-Student2 小时前
【ubuntu】在Linux系统上安装Microsoft Edge浏览器
linux·ubuntu·microsoft
谅望者2 小时前
Linux文件查看命令完全指南:cat、less、head、tail、grep使用详解
linux·excel·less·shell·文件操作·命令行·系统运维
casdfxx3 小时前
blender实现手柄控制VR视角
linux·vr·blender
盼哥PyAI实验室3 小时前
纯前端打造个人成长网站:零后端、零部署、零服务器的实践分享
运维·服务器·前端·javascript·echarts·个人开发