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

相关推荐
大眼、不聚光10 分钟前
9.linux系统管理-ssh免密配置
linux·ssh·github
啦啦啦!15 分钟前
虚拟机如何接入Codex并配置中转站(vscode插件版)
linux·vscode·ubuntu·编辑器
love530love15 分钟前
Ubuntu系统通过Homebrew安装Lightpanda完整实战教程(含端口占用排坑)
大数据·linux·运维·人工智能·elasticsearch·搜索引擎
淮北49419 分钟前
如何服务器和本地实现挂载
运维·服务器
j7~20 分钟前
【Linux】二十八.线程篇五《Linux多线程编程:线程同步之条件变量》---详解
linux·运维·c++·学习·条件变量·线程同步
木易 士心28 分钟前
服务器构建指南:从选型、部署到高可用架构详解
运维·服务器·后端·架构
似水এ᭄往昔35 分钟前
【Linux网络编程】--UDP__SCOKE
linux·计算机网络
码农阿豪1 小时前
多台服务器还在反复输SSH?用EasyNode集中管理连接
运维·服务器·ssh
进击切图仔1 小时前
Ubuntu 挂载 XFS 磁盘并免密读写
linux·网络·ubuntu
qetfw1 小时前
Debian Apache2 执行 Shell CGI:动态时间页面配置与验证
linux·运维·debian