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

相关推荐
liulilittle11 分钟前
什么是“单流”?一个服务器上能不能同时存在多个“单流”?
服务器·网络·tcp/ip·计算机网络·信息与通信·tcp·通信
无限进步_17 分钟前
【Linux】环境变量:系统运行参数的“备忘录”
linux·运维·服务器
YYRAN_ZZU19 分钟前
Ubuntu22.04搭建QEMU嵌入式开发环境全攻略
linux·嵌入式硬件·ubuntu
无忧.芙桃19 分钟前
Linux信号机制(上)
linux·运维·服务器
kongba00722 分钟前
WSL与VMware冲突
linux
utf8mb4安全女神35 分钟前
怎么让服务器给自己的邮箱发消息【shell脚本】
linux·运维·服务器
ZYH_060135 分钟前
BGP策略作业
运维·服务器
San813_LDD1 小时前
[操作系统]虚拟机技术
linux·windows
你是个什么橙1 小时前
安装KVM服务器、使用libvirt tools工具管理虚拟机
运维·服务器·云计算
.千余1 小时前
【C++】 String 常用操作:增删查改 | 查找 | 截取 | IO
java·服务器·开发语言·c++·笔记·学习