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

相关推荐
红姐跨境书13 分钟前
Facebook账号频繁被要求验证怎么办?从登录异常到浏览器环境排查
运维·服务器
叼烟扛炮19 分钟前
Linux 学习笔记(八):GCC 编译器的四大阶段与链接原理
linux·笔记·学习
月落汀兰29 分钟前
服务器出问题没人告警?Zabbix6.0 LNMP 部署,MySQL 监控 + 邮件告警保姆级实战
运维·服务器·zabbix
运维踩坑日记29 分钟前
【无标题】
linux·运维·windows
xiaoyunxuexibiji30 分钟前
供应商绩效评价体系如何建立?从评价指标到自动化考核
运维·人工智能·自动化
Z21-X31 分钟前
云原生运维学习笔记:从云计算概念到服务器硬件的入门之旅
运维·笔记·学习·云原生
虚无的纽扣39 分钟前
【Linux】线程(下):怎么创建、终止与分离一个线程
linux·运维·服务器
叼烟扛炮41 分钟前
Linux 学习笔记(七):软件安装与 Vim 编辑器极速入门
linux·学习·编辑器
墨神谕44 分钟前
Docker 数据卷(Volume)详解:Dockerfile 声明、Docker Compose 挂载宿主机目录及持久化实战
运维·docker·容器
金色熊族1 小时前
在Qt中使用asan检测内存泄漏(二)--检查依赖库的内存使用
linux·qt·asan