1.ansible的安装

yum -y install ansible

2.然后就是配置/etc/ansible/hosts,里面主要记载着你登录的ip或者别名以及登录时的用户以及密码,根据需求我这里把所有主机分为2类,[web]和[orther]

# cp /etc/ansible/hosts /etc/ansible/hosts-   #操作前备份

# vi web                          #创建web文件,记载主机ip以及密码(以下为例)

1.2.3.4   asdasd

2.4.5.6   sdsdfg

4.5.7.8   asdasd

7.5.3.4   asdafg


# vi ansible_web.sh                   #配置把ip和密码按格式批量输       

echo "[web]" >>/etc/ansible/hosts          入/etc/ansible/hosts脚本

for i in `cat web|awk '{print $1}'`

do

pass=`cat web|grep "$i"|awk '{print $2}'`

echo "$i ansible_ssh_user=root ansible_ssh_pass=$pass >> /etc/ansible/hosts

done


# bash +x ansible_web.sh 

# vi /etc/ansible/hosts                    #查看脚本是否实现成功

db-[99:101]-node.example.com

[web]

1.2.3.4 ansible_ssh_user=root ansible_ssh_pass=asdasd

2.4.5.6 ansible_ssh_user=root ansible_ssh_pass=sdsdfg

4.5.7.8 ansible_ssh_user=root ansible_ssh_pass=asdasd

7.5.3.4 ansible_ssh_user=root ansible_ssh_pass=asdafg #成功


# vi orther                             #创建orther文件记载web以外的主机

1.1.1.1  asdasd                           的ip和root登录密码

1.1.1.2  asdasd

1.1.1.3  asdasa

1.1.1.4  asdfas


# cp ansible_web.sh  ansible_orther.sh         

# sed -i   's/web/orther/g' ansible_orther.sh     #配置orther批量输入ip和密码以及

# bash +x ansible_orther.sh                  登录格式到/etc/ansible/hosts


# vi /etc/ansible/hosts                    #验证是否成功键入

4.5.7.8 ansible_ssh_user=root ansible_ssh_pass=asdasd

7.5.3.4 ansible_ssh_user=root ansible_ssh_pass=asdafg

[orther]

1.1.1.1 ansible_ssh_user=root ansible_ssh_pass=asdasd

1.1.1.2 ansible_ssh_user=root ansible_ssh_pass=asdasd

1.1.1.3 ansible_ssh_user=root ansible_ssh_pass=asdasa

1.1.1.4 ansible_ssh_user=root ansible_ssh_pass=asdfas


  nice!很显然键入成功,于是ansible环境已搭好,你可以尽情的批量处理这些主机了!!