所有步骤均在ansible server主机上执行。
一、安装Ansible
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
或者 yum install epel-release -y
yum install ansible -y
二、ansible 到workder节点免密登录
2.1 生成公钥
ssh-keygen -t rsa
2.2 公钥拷贝到woker节点
<1> 客户机比较少可以直接执行以下指令
ssh-copy-id root@20.20.45.177
<2> 或者用脚本批量执行
sh copy_ssh_key.sh
其中脚本内容如下:
yum install expect -y
#cat list
20.20.45.177 20.20.41.213
#cat copy_ssh_key.sh
#!/bin/bash while read line do user="root" ip=`echo $line | cut -d " " -f 1` passwd="1" echo ip: $ip echo pw: $passwd expect <<EOF set timeout 2s spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user@$ip expect { "yes/no" { send "yes\n"; exp_continue } "password" { send "$passwd\n" } } expect "password" { send "$passwd\n" } EOF done < list
3、设置inventory
cat /etc/ansible/hosts
[client01] 20.20.45.177 [client02] 20.20.41.213
4、验证测试,(不能在项目目录下测试)