Linux互信shell脚本\ansible

因为之后要用ansible,所以,机器之间要做做个互信,写个shell脚本节省人力。下面两个都一样,用哪个都行。

/root目录下运行ssh-key命令,一路回车

ssh-key

SHELL脚本1:

#!/bin/bash

#熊の力量 编写,实现基于KEY验证的脚本方式2,添加互信。

IPLIST="
"
rpm -qa sshpass &> /dev/null || yum install -y sshpass
[ -f /ssh/.ssh/id_rsa] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=密码
for IP in $IPLIST;do
    sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done

SHELL脚本2:

#!/bin/bash

#熊の力量 编写,实现基于KEY验证的脚本,添加互信。
rpm -qa sshpass &> /dev/null || yum install -y sshpass
[ -f /ssh/.ssh/id_rsa] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=密码
while read IP;do
    sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done < hosts.list

ansible脚本:

[root@test ansible]# pwd
/etc/ansible

[root@test ansible]# cat playbook/authorized_key.yaml
- name: 互信
  ignore_errors: false
  gather_facts: false
  hosts: rhel8    #/etc/ansible/hosts内写的标签
  tasks:
    - name: authorized_key
      authorized_key:
        user: root    #需要做互信的用户
        state: present    #新增公钥内容到服务器用户家目录的.ssh目录的authorized_keys文件
                          #没有则创建authorized_keys文件state: (1) present 添加 (2) absent 删除
        key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"

ansible脚本执行:

ansible-playbook -i hosts playbook/authorized_key.yaml -k

使用-k参数或者如下图文件中写ansible_ssh_pass密码

[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# vim hosts
[db]
192.168.10.11 ansible_ssh_user='root' ansible_ssh_pass='123456'
192.168.10.12 ansible_ssh_user='root' ansible_ssh_pass='123456'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值