安装
yum -y install ansible expect
生成key,ssh-keygen -t rsa -f ~/.ssh/id_rsa
两种办法
- ssh+expect 出自oldbody
cat /etc/ansible/hosts
[web]
web1 ansible_ssh_host=192.168.1.21
web2 ansible_ssh_host=192.168.1.22
提供修改的exp.sh
. /etc/init.d/functions ip=$1 function KNOWN_HOST_REBUILD() { [ ! -e ~/.ssh/known_hosts ] && mkdir -p ~/.ssh/ && touch ~/.ssh/known_hosts local i=$1 sed -i "/^${i} /d" ~/.ssh/known_hosts expect -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=$1 expect -c " set timeout -1 spawn ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]$ip expect \"*password:\" send \"你的密码\r\" expect eof" } KNOWN_HOST_REBUILD $1 PASS_PASSWD $1
使用方法:./exp.sh ip,就会自动建立ssh了,然后ansible各种命令测试
2.authorized_key模块
基础资料
http://docs.ansible.com/authorized_key_module.html
简单例子
# 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="你的密码"
简单的使用
cat rsync_key.ymal --- - hosts: web3 user: root tasks: - name: ssh authorized_key: user="root" key="{{ lookup(‘file‘, ‘/root/.ssh/id_rsa.pub‘) }}"
使用方法:ansible-playbook rsync_key.ymal
出自马哥
http://mageedu.blog.51cto.com/4265610/1412028
authorized_key ymal格式不会写,Google到的
(Ansible Cookbook 2014) http://ansiblecookbook.com/html/en.html
时间: 2024-11-09 00:14:42