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

相关推荐
Sumlll_19 分钟前
Ubuntu系统下QEMU的安装与RISC-V的测试
linux·ubuntu·risc-v
猫头虎30 分钟前
2025最新OpenEuler系统安装MySQL的详细教程
linux·服务器·数据库·sql·mysql·macos·openeuler
木子.李3471 小时前
ssh连接远程服务器相关总结
运维·服务器·ssh
BD_Marathon2 小时前
SpringBoot——辅助功能之切换web服务器
服务器·前端·spring boot
晚风吹人醒.2 小时前
SSH远程管理及访问控制
linux·运维·ssh·scp·xshell·访问控制·远程管理
Uncertainty!!4 小时前
Linux多用户情况下个别用户输入密码后黑屏
linux·远程连接
necessary6534 小时前
使用Clion查看linux环境中的PG源码
linux·运维·服务器
小猪佩奇TONY5 小时前
Linux 内核学习(14) --- linux x86-32 虚拟地址空间
linux·学习
Lam㊣5 小时前
Centos 7 系统docker:更换镜像源
linux·docker·centos
FL16238631295 小时前
win11+WSL+Ubuntu-xrdp+远程桌面闪退+黑屏闪退解决
linux·运维·ubuntu