Ansible Automatic SSH

Source: Internet
Author: User

Installation

Yum-y Install ansible expect

Generate Key,ssh-keygen-t rsa-f ~/.ssh/id_rsa

Two ways

    1. Ssh+expect from Oldbody

Cat/etc/ansible/hosts

[web]

Web1 ansible_ssh_host=192.168.1.21

WEB2 ansible_ssh_host=192.168.1.22

Exp.sh to provide modified

. /etc/init.d/functionsip=$1function known_host_rebuild () {[  ! -e ~/.ssh/known_hosts ] && mkdir -p ~/.ssh/ &&  touch ~/.ssh/known_hostslocal i=$1sed -i  "/^${I} /D"  ~/.ssh/known_ hostsexpect -c  "Spawn /usr/bin/ssh [email protected]${i} echo ok;expect  \ "*yes/no", "send \" yes\r\ ";expect eof "  return 0[[ $? -ne 0  ]] && echo  "$i  know host rebuild fail,maybe the  Server connect error "}function pass_passwd () {ip=$1expect -c " set timeout - 1spawn ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] $ipexpect  \ "* Password:\ "send \" Your password \r\ "expect eof"  }known_host_rebuild $1pass_passwd $1 

How to use:./exp.sh IP, SSH is automatically established, and then ansible various command tests

2.authorized_key Module

Basic information

http://docs.ansible.com/authorized_key_module.html

Simple example

# Example using key data from a local file on the management Machine-authorized_key:user=charlie key= "{{lookup (' file '), '/home/charlie/.ssh/id_rsa.pub ')}} "

Cat/etc/ansible/hosts

[Unknow]

WEB3 ansible_ssh_user=root ansible_ssh_host=192.168.1.21 ansible_ssh_pass= "Your password"

Simple to use

Cat Rsync_key.ymal----hosts:web3 user:root tasks:-name:ssh authorized_key:user= "root" key= "{{lookup (' file '), '/root/.ssh/id_rsa.pub ')}} "

How to use: Ansible-playbook rsync_key.ymal

Originated from Marco

http://mageedu.blog.51cto.com/4265610/1412028


Authorized_key ymal format does not write, Google to the

(Ansible Cookbook) http://ansiblecookbook.com/html/en.html

This article is from the "One Sky" blog, make sure to keep this source http://szgb2014.blog.51cto.com/340201/1669731

Ansible Automatic SSH

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.