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

相关推荐
暴力求解1 天前
Linux---线程基础
linux·运维·服务器
JAVA面经实录9171 天前
计算机基础(完整版·超详细可背诵)
java·linux·数据结构·算法
晚风_END1 天前
Linux|操作系统|最新版openzfs编译记录
linux·运维·服务器·数据库·spring·中间件·个人开发
GanGanGanGan_1 天前
Rocky Linux 9 + XFCE 编译安装 fcitx5 + Rime 教程
linux·centos
代码中介商1 天前
Linux TCP 网络编程完全指南:从三次握手到高并发服务器
服务器·网络·tcp/ip
咖喱o1 天前
QinQ/VLAN Stacking
linux·运维·服务器·网络
xiaoye-duck1 天前
《Linux系统编程》Linux指令 (二):从零开始理解Linux基础指令
linux
月白风清江有声1 天前
安装mingw 版 cmake
linux
sduwcgg1 天前
IQ-Learn 在 RTX 3090 服务器上的环境配置与踩坑记录
运维·服务器
呱呱巨基1 天前
Linux 基础IO
linux·c++·笔记·学习