linux 批量查看机器配置
(1)、ansible的安装
找台管理端安装ansible软件
Ansible是属于Extra Packages for Enterprise Linux (EPEL)库的一部分,因此要先安装EPEL
# yum install epel-release
# yum repolist
yum安装时出现:Cannot retrieve metalink for repository: epel. Please verify its path and try again
解决办法
把/etc/yum.repos.d/epel.repo,文件第3行注释去掉,把第四行注释掉。
# yum clean all
# yum install ansible
(2)、配置ansible免密登录受控主机 (只需在管理端主机操作)
批量拷贝管理机的公钥到受控节点服务器上去
2.1 准备受控端主机文件 , 格式 “ ip 空格 root的密码”
比如我的:
# cat hosts
222.186.43.145 a123456
222.186.169.73 a123456
222.186.169.61 a123456
222.186.169.13 a123456
2.2 编写copy-key.sh 脚本,脚本内容如下
1 2 3 4 5 6 7 8 9 10 11 | #!/bin/bash # Copy ssh public key to remote servers yum install openssh-clients -y yum install sshpass rm -rf /root/.ssh/id_rsa* ssh-keygen -q -f ~/.ssh/id_rsa -t rsa -N '' ip=$(cat /root/hosts |tr -s ' '|cut -f1 -d ' ') password=$(cat /root/hosts |tr -s ' '|cut -f2 -d ' ') cat /root/hosts | while read ip password; do /usr/bin/sshpass -p $password ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip -o StrictHostKeyChecking=no done |
2.3 执行
# chmod 0755 /root/copy-key.sh
# ./root/copy-key.sh
此时, 可以在管理机上使用root用户ssh免密码登录受控端节点服务器了
(3)、应用脚本的编写 (只需在管理端主机操作)
3.1
编写应用脚本 /root/host_src.sh 作为基础脚本, ansible将调用此脚本
批量查看受控服务器的硬件配置信息 ,包括 “受控端服务器IP cpu型号 物理CPU个数 单颗cpu核心数 逻辑cpu数量 磁盘容量 内存大小 ”
host_src.sh 脚本内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/bin/bash ip=` ifconfig | awk 'BEGIN{FS=" "} NR==2{print $2}'` cpu_mode=`grep 'model name' /proc/cpuinfo|uniq|cut -d: -f2|tr -s ' '` count=`cat /proc/cpuinfo| grep 'physical id'| sort| uniq| wc -l` cpu_core=`cat /proc/cpuinfo| grep 'cpu cores'| uniq|awk -F : '{print $2}'` cpus=`cat /proc/cpuinfo| grep 'processor'| wc -l` disk=`lsblk |grep disk | awk 'CONFIG {FS=" "} NR==1{print $4}'` mem=`free -m | grep 'Mem'|cut -f2 -d :| tr -s ' ' | awk '{print $1}'` echo " ${ip} Cpu型号: $cpu_mode 物理CPU个数: $count 单颗CPU核心数:$cpu_core 逻辑CPU数量:$cpus 磁盘容量: $disk 内存容量: $mem MB " |
3.2 编写ansible的ad-hoc 脚本,脚本名字叫 /root/display-host.sh , 该脚本引用host_src.sh脚本
display-host.sh 的脚本内容
#!/bin/bash
ansible nodes -m shell -a "yum -y install libselinux-python"
ansible nodes -m copy -a 'src=/root/host_src.sh dest=/root mode=0755'
ansible nodes -m shell -a '/root/host_src.sh >/root/host.txt'
ansible nodes -a 'cat /root/host.txt'
3.3 执行脚本 (在管理端主机操作)
# chmod 0755 /root/display-host.sh
# ./root/display-host.sh
此时,可以看到受控端节点服务器的硬件信息打印在屏幕上