Installation
Yum-y Install ansible expect
Generate Key,ssh-keygen-t rsa-f ~/.ssh/id_rsa
Two ways
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