MySQL--Ansible推送密钥实现免密码登录

栏目: 服务器 · 发布时间: 8年前

内容简介:MySQL--Ansible推送密钥实现免密码登录

从别人网站抄过来,保留自用

场景: 对于需要管理的很多 linux 服务器,每次登录都输入密码比较痛苦,配置一个跳板机,将本地公钥推送带各访问节点上实现SSH登录

登录账号:admin

本地秘钥路径:/home/admin/.ssh/id_rsa.pub

=======================================================

首先在跳板机上配置秘钥

ssh-keygen -t rsa -P "" -f ~/.ssh/id_dsa

然后创建ansible的playbook文件create_admin.yaml

- name: Linux Create User and Upload User Public keys
  hosts: all
  #remote_user: xxxx
  #sudo: yes
  vars:
      create_user_name: admin
  tasks:
    - name: Make sure we have a 'admin' group
      group:
        name: admin
        state: present
 
    - name: Allow 'admin' group to have passwordless sudo
      lineinfile:
        dest: /etc/sudoers
        state: present
        regexp: '^%admin'
        line: '%admin ALL=(ALL) NOPASSWD: ALL'
 
    - name: Create user {{ create_user_name }}
      user:
        name: "{{ create_user_name }}"
        shell: /bin/bash
        groups: admin
        createhome: yes
        home: /home/{{ create_user_name }}
        state: present
 
    - name: create key directory
      action: file path=/home/{{ create_user_name }}/.ssh/ state=directory  owner={{ create_user_name }} group={{ create_user_name }} mode=0700
 
    - name: create key file
      action: file path=/home/{{ create_user_name }}/.ssh/authorized_keys state=touch  owner={{ create_user_name }} group={{ create_user_name }} mode=0600
        
 
    - name: Set authorized key took from file
      authorized_key:
        user: "{{ create_user_name }}"
        state: present
        key: "{{ lookup('file', '/home/admin/.ssh/id_rsa.pub') }}"

然后使用ansible执行推送

=======================================================

登录时直接使用ssh登录,如:

ssh 192.168.166.170

大神同事将下面脚本脚本封装成go.sh,放入到/bin目录下:

host=$1
if [ -z ${host} ]
then
   host='127.0.0.1'
fi
ssh admin@${host} -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa

这样就可以快速地使用go.sh +ip登录指定服务器

=======================================================

MySQL--Ansible推送密钥实现免密码登录


以上所述就是小编给大家介绍的《MySQL--Ansible推送密钥实现免密码登录》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pattern Recognition and Machine Learning

Pattern Recognition and Machine Learning

Christopher Bishop / Springer / 2007-10-1 / USD 94.95

The dramatic growth in practical applications for machine learning over the last ten years has been accompanied by many important developments in the underlying algorithms and techniques. For example,......一起来看看 《Pattern Recognition and Machine Learning》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码