又一个ansible例子

这个例子有点复杂,他在被控端上采集CPU 内存和磁盘利用率,并且以模板的形式保存在被控端,最后再把这个结果文件从被控端取回来。综合用到了shell、register、template和fetch4个模块


  • name: get cpu mem and disk

hosts: all

tasks:

  • name: get cpu

shell:

cmd: top -bn 1 | grep Cpu | awk '{print 2+3}'

register: mycpu

  • name: get mem

shell:

cmd: free | grep Mem | awk '{print 3/2}'

register: mymem

  • name: get disk

shell:

cmd: df / | grep / | awk '{print $5}'

register: mydisk

  • name: output template

template:

src: /root/result.j2

dest: /root/result.txt

  • name: fetch result

fetch:

src: /root/result.txt

dest: /root/

flat: yes

模板文件如下:

more result.j2

cpu useage: {{mycpu.stdout}}

mem useage: {{mymem.stdout}}

disk useage: {{mydisk.stdout}}

相关推荐
babytiger2 小时前
Gitea 重安装 + Snap 数据迁移完整流程总结
linux·elasticsearch·gitea
匆匆那年9672 小时前
远程 Linux 校园网认证操作手册(本地浏览器法)
linux·运维·服务器
dog2503 小时前
为何新增网络路径反而引入额外时延
服务器·网络·php
newnazi3 小时前
RedHat10 安装MS SQL Server2025
linux·服务器·数据库
zkkkkkkkkkkkkk3 小时前
Linux进行管理工具Supervisor配置与使用
linux·python·supervisor
开发者联盟league4 小时前
linux普通用户使用pip安装模块
linux·python·pip
QuestLab4 小时前
③-进阶篇:vLLM实战——多卡部署、压测与排障
linux·服务器·网络
Languorous.4 小时前
Windows 安装 Linux 虚拟机 / WSL 完整教程(新手零失败)
linux·运维·windows
颂love5 小时前
Linux命令的简单学习
linux·运维·学习
satadriver5 小时前
ip-guard网络通信加解密
linux·网络·tcp/ip