Ansible安装
yum install ansible -y
生成秘钥
ssh-keygen -t RSA
推送公钥给node节点 小数量操作例子
ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.0.72 #操作第一个
ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.0.73 #操作第二个
批量推送秘钥例子
# 解决提示主机指纹认证问题
-o StrictHostKeyChecking=no
# 解决密码输入问题:执行yum安装sshpass
yum install sshpass -y
sshpass -p 服务器密码
命令脚本
for i in {72,73};
do
sshpass -p 123456 ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.0.$i -o StrictKeyChecking=no
done
常用操作
ansible node1 -m ping #ping使用
ansible node1 --list-host # 查看组中节点
host写法
#写法一
[组名]
172.16.0.72
172.16.0.73
172.16.0.74
172.16.0.75
[组名:vars] #传参数
ansible_ssh_password=1
#写法二 起别名
[组名]
node01 ansible_ssh_host=172.16.0.72 ansible_ssh_user=root ansible_ssh_password=1 #通过什么用户执行操作
node02 ansible_ssh_host=172.16.0.73 ansible_ssh_user=root ansible_ssh_password=1 ansible_ssh_port=8022
node03 ansible_ssh_host=172.16.0.74 ansible_ssh_user=root ansible_ssh_password=1
node04 ansible_ssh_host=172.16.0.75 ansible_ssh_user=root ansible_ssh_password=1
[组名:children]
组1
组2