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

相关推荐
CheungChunChiu2 小时前
Linux 内核设备模型与驱动框架解析 ——以 rk-pcie 为例
linux·运维·ubuntu
列逍3 小时前
Linux进程(三)
linux·运维·服务器·环境变量·命令行参数
水天需0103 小时前
VS Code Ctrl+Shift+V 预览 Markdown 无效的解决方案
linux
赖small强5 小时前
【Linux C/C++开发】Linux 平台 Stack Protector 机制深度解析
linux·c语言·c++·stack protector·stack-protector·金丝雀机制
陌路206 小时前
Linux42 守护进程
linux
liteblue6 小时前
DEB包解包与打包笔记
linux·笔记
minji...7 小时前
Linux 基础IO(一) (C语言文件接口、系统调用文件调用接口open,write,close、文件fd)
linux·运维·服务器·网络·数据结构·c++
赖small强7 小时前
【Linux内存管理】Linux虚拟内存系统详解
linux·虚拟内存·tlb
码龄3年 审核中7 小时前
Linux record 04
linux·运维·服务器
RisunJan7 小时前
Linux命令-ftptop命令(实时监控 ProFTPD 服务器连接状态)
linux·运维·服务器