Ansible免密登录

Ansible免密登录

关闭公钥认证的方法有两种:

  • 编辑ansible.cfg配置文件

[defaults]
host_key_checking = False
1
2
直接设置环境变量
命令为:
export ANSIBLE_HOST_KEY_CHECKING=False
1
2
(2)使用ssh-key产生公钥和私钥
[root@localhost ansible]# ssh-keygen -t rsa -b 2048 -P ” -f /root/.ssh/id_rsa

(3)添加主机信息到hosts文件中
[root@localhost ansible]# pwd
/etc/ansible
[root@localhost ansible]# vim hosts
[db]
172.25.70.1 ansible_ssh_user=“root” ansible_ssh_pass=“redhat”
172.25.70.2 ansible_ssh_user=“root” ansible_ssh_pass=“redhat”

##如果用户名和密码都相同的话也可以不用添加,在执行第5步的时候加上-u和-k就可以了

(4)编写Playbook剧本文件/命令
##是基于YAML语言编写的
ansible -i hosts all -m authorized_key -a “user=root key=’{{ lookup(‘file’, ‘/root/.ssh/id_rsa.pub’)}}’ path=’/root/.ssh/authorized_keys’ manage_dir=no” --ask-pass -c paramiko
[root@localhost ansible]# vim ssh.yml

ssh-addkey.yml


  • hosts: all
    gather_facts: no

    tasks:

    • name: install ssh key
      authorized_key: user=root
      key="{{ lookup(‘file’, ‘/root/.ssh/id_rsa.pub’) }}"
      state=present

(5)运行playbook文件
[root@localhost ansible]# ansible-playbook -i hosts ssh.yml

(6)测试
此时运用模块或者直接ssh登录主机都不需要密码

原文:https://blog.csdn.net/lcl_xiaowugui/article/details/81874015
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_15282237/article/details/86487822