收集配置脚本
[xx@zax ~]$ cat test.sh
echo -e '内存:' '\c'&& echo -e $(free -m |grep Mem|awk '{print $2}') '\c' && echo -e '磁盘:' '\c '&& echo -e $(lsblk |grep "root" |awk '{print $5}'|uniq) '\c '&&echo -e 'CPU:' '\c';echo -e $(cat /proc/cpuinfo |grep process|wc -l) '\c'
内存: 5852 磁盘: 93G CPU: 4
使用ansible批量获取:
[xx@zax ~]$ ansible server -m script -a "/root/test.sh" > ziyuan
192.168.6.3 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.6.3 closed.\r\n",
"stdout": "内存: 11911 磁盘: 50G CPU: 6 ",
"stdout_lines": [
"内存: 11911 磁盘: 50G CPU: 6 "
]
}
192.168.6.2 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.6.2 closed.\r\n",
"stdout": "内存: 11911 磁盘: 50G CPU: 6 ",
"stdout_lines": [
"内存: 11911 磁盘: 50G CPU: 6 "
]
}
过滤ip和资源配置信息,合并成一行
[xx@zax ~]$ cat ziyuan |egrep 'SUCCESS|\"stdout\"'|awk '{if(NR%2==0){printf $0 "\n"}else{printf "%s:",$0}}' > ziyuan1
192.168.6.3 | SUCCESS => { "stdout": "内存: 11911 磁盘: 50G CPU: 6 ",
192.168.6.2 | SUCCESS => { "stdout": "内存: 11911 磁盘: 50G CPU: 6 ",
截取ip 和 资源配置信息
[xx@zax ~]$ cat ziyuan1 |awk -F 'SUCCESS => {: "stdout":' '{print $1,$2}' |sed 's/,//'
192.168.6.3 | "内存: 11911 磁盘: 50G CPU: 6 "
192.168.6.2 | "内存: 11911 磁盘: 50G CPU: 6 "