这个例子有点复杂,他在被控端上采集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}}