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

相关推荐
三言老师14 小时前
秘诀-如何远程SSN访问家里的八台电脑(frp 实操方案)
linux·运维·ssh
躺不平的理查德15 小时前
ARCH与交叉编译器的关系
服务器·arm
举手16 小时前
Dispatcher模块剖析
linux·c++
谢慧琼17 小时前
2026零基础做企业网站,低成本搭建官方网站
服务器·前端·javascript
allforgood17 小时前
运行容器
linux
Light_It18 小时前
Linux 内核参数 pci-stub.ids=
linux·kernel
RisunJan18 小时前
Linux命令-scriptreplay(终端会话回放)
linux·运维·chrome
spencer_tseng18 小时前
[kylin & linux] install docker
linux·docker·kylin
x²+(y-√³x²)²=119 小时前
Linux打包文件到Windows,文件/文件类型丢失
linux·运维·windows
世人万千丶20 小时前
去重插入与超限淘汰:ArkTS 实现鸿蒙搜索历史的 LIMIT 艺术
运维·服务器·学习·华为·harmonyos·鸿蒙