又一个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}}

相关推荐
ggaofeng10 分钟前
如何通过uboot加载硬盘
linux·qemu·uboot
尔染君子15 分钟前
嵌入式Linux驱动开发(按键驱动)
linux·驱动开发
kebidaixu28 分钟前
BCU 平台 Modbus 主机功能开发:液冷机组 & 消防传感器
linux
vsropy38 分钟前
安装虚拟机VMware
linux·windows
Jason_chen40 分钟前
Linux 3.0 串口机制深度解析:传统8250驱动与基础RS-232/485支持
linux·前端
Jason_chen41 分钟前
Linux 5.10 串口机制深度解析:serial_core重构与RS-485自动方向控制革命
linux
无敌的牛1 小时前
自省。。。。
linux
lqjun08271 小时前
Linux 下 Hermes Agent 代理配置不生效问题的解决
linux·服务器
Gary Studio1 小时前
复杂 SoC(RK3568)PCB 布局的五步
android·linux·硬件
一拳一个娘娘腔1 小时前
CVE-2026-43284 — Dirty Frag 深度拆解:当零拷贝遇上原地解密,页缓存成了攻击者的画板
linux·缓存