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

相关推荐
郝学胜-神的一滴2 小时前
Linux命名管道:创建与原理详解
linux·运维·服务器·开发语言·c++·程序人生·个人开发
宾有为2 小时前
【Linux】Linux 常用指令
linux·服务器·ssh
wanhengidc2 小时前
云手机性能如何?
运维·服务器·科技·智能手机·云计算
wdfk_prog2 小时前
[Linux]学习笔记系列 -- [block]bio
linux·笔记·学习
ajassi20002 小时前
开源 Linux 服务器与中间件(十三)FRP服务器、客户端安装和测试
linux·服务器·开源
Hellc0072 小时前
Docker部署BaGet私有NuGet服务器:从入门到备份恢复完整指南 二
服务器·docker·eureka
XH-hui3 小时前
【打靶日记】群内靶机vm1
linux·网络安全
Eric.Lee20214 小时前
ubuntu 安装 Miniconda
linux·运维·python·ubuntu·miniconda
会飞的土拨鼠呀4 小时前
通过Linux进程id找到程序路径
linux·服务器·网络
8***84825 小时前
如何在Linux中找到MySQL的安装目录
linux·运维·mysql