#!/bin/bash
#just prevent you have ssh key
rm -rf /root/.ssh/id_dsa* >> /dev/null 2>&1
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P "" >> /dev/null 2>&1
#加载repo源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo && \
#all can ssh each other
yum install ansible -y >> /dev/null 2>&1 && \
echo "install ansible"
#judge you ansible install
if [ "`rpm -qa | grep -o ansible`" == "ansible" ]
then
for ip in $*
do
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@$ip" >> /dev/null 2>&1
#just for you Determine whether distribution of success of ssh
export a=$(ssh $ip hostname -I|awk '{print $1"|"$2}')
export b=$(cat /etc/hosts | grep $ip | awk '{print $1}')
done
if [[ "$b" =~ $a ]]
then
#insert you group of ansible
read -p "pleace insert you ansible group name: " grop
read -p "pleace insert The name of the group members just like one two ...: " arg
cat << EOF >> /etc/ansible/hosts
[$grop]
`echo $arg | tr " " "\n"`
EOF
#judge you can use ansible
ansible $grop -m command -a "hostname" 2>> /var/log/ansible.log
if [ $? -ne 0 ]
then
echo "ERROR:you can not use ansible,pleace see /var/log/ansible.log"
else
echo "congratulations!!you ansible successful! and now you can do you want"
fi
else
echo "you fenfa falit"
fi
else
echo "error:you can not install ansible"
fi
转载于:https://blog.51cto.com/shuai12138/1907738