02 ansible核心模块

shell模块(万能模块)可以使用特殊符号 < > | 等 command不能

ansible 192.168.0.102 -m shell -a "hostname"
ansible 192.168.0.102 -m shell -a "echo 123 > haha.txt"
ansible 192.168.0.102 -m shell -a "netstat -anptu | grep xxx"

实践使用 利用shell执行脚本
第一个步骤:编写一个脚本
第二个步骤:将脚本发送到远程主机
第三个步骤:将脚本权限进行修改(添加执行权限)
第四个步骤:运行ansible命令执行脚本

eg:执行检查磁盘脚本

#!/bin/bash
basedir=$(cd `dirname $0`;pwd)
echo $basedir
diskmax=10 # 磁盘的阈值

function check_max(){
   local disk_size=$1
   if [ $disk_size -ge $diskmax ]
   then
      echo "unhealth"
   else
      echo "health"
   fi
}

function check_disk_info(){
#grep -v 取反 # cut -d '%' -f 1 以%为分隔符选择第一个 #while read disk_size  循环读入每行数据 赋值给disk_size
   df -h | grep -v /dev/loop0 | grep -v /dev/sr0 | grep dev | awk 'NR > 1 {print $5}' | cut -d '%' -f 1 | while read disk_size  
   do
        echo ""
        echo "disk_size=$disk_size%"
        check_max $disk_size
   done
}

check_disk_info

猜你喜欢

转载自www.cnblogs.com/linux985/p/11302136.html