ansible 自动ssh
http://szgb2016.blog.51cto.com/340201/1669731
安装
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
. /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 root@${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 root@$ipexpect \"*password:\"send \"你的密码\r\"expect eof" }KNOWN_HOST_REBUILD $1PASS_PASSWD $1 |
使用方法:./exp.sh ip,就会自动建立ssh了,然后ansible各种命令测试
2.authorized_key模块
分两步走
1.known_host
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
. /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 root@${i} echo ok;expect \"*yes/no)?\";send \"yes\r\";expect \"*?assword:*\"send -- \"\003\r\"expect eof " return 0[[ $? -ne 0 ]] && echo "$i know host rebuild fail,maybe the server connect error"}KNOWN_HOST_REBUILD $1 |
2.编写简单的yml
基础资料
http://docs.ansible.com/authorized_key_module.html
简单例子
|
1
2
|
# 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="你的密码"
简单的使用
|
1
2
3
4
5
6
7
|
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
网上的例子
第一种方式:手动安装
cd ~/Downloads
curl -O -L http://downloads.sourceforge.net/project/sshpass/sshpass/1.05/sshpass-1.05.tar.gz
tar xvzf sshpass-1.05.tar.gz
cd sshpass-1.05
./configure
make
sudo make install
```
第二种方式,安装Mac os包管理器之外第三方包
brew install https://raw.github.com/eugeneoden/homebrew/eca9de1/Library/Formula/sshpass.rb
3.配置文件
/etc/ansible/hosts — 默认资源文件
/usr/share/ansible/ — 默认模块库
/etc/ansible/ansible.cfg — 默认配置文件
~/.ansible.cfg — 用户配置文件,如果使用优先级高于ansible.cfg配置文件
具体配置文件参考:http://docs.ansible.com/intro_configuration.html#pipelining
step2:远程执行命令
ansible是基于ssh协议之上进行远程管理,所以无须安装客户端,直接只要能ssh连接过去,就可以进行管理了
1.配置管理客户端和秘钥登录
cp hosts hosts.bak
vim hosts [test] //group_name,名称可以自定义,可以将不同的作用的机器放在不同的组里
192.168.122.134
192.168.122.131 [test]
192.168.122.134 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=skstserver #ssh无秘钥登录ssh-keygen -t rsa -P ''ssh-copy-id -i .ssh/id_rsa.pub root@192.168.122.131
2、批量发送公钥
为ansible开始管理节点传递公钥
如果是可以直接使用root的环境,则直接使用这个脚本
vim tra_pub.exp
#!/usr/bin/expect -f
set timeout -1
set user root
set passwd "123456" for { set i 201 } { $i < 208 } { incr i } {
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user@192.168.1.$i
expect {
"yes/no" { send "yes\r";exp_continue }
"id_rsa" { send "yes\r";exp_continue }
"*assword" { send "$passwd\r" }
}
}
expect eof
如果需要使用普通用户进行切换的环境则可以使用以下的脚本
#!/usr/bin/expect -f
set timeout -1
set user test
set passwd "123456" for { set i 100 } { $i < 120 } { incr i } {
spawn ssh -l $user 172.41.30.$i
expect {
"yes/no" { send "yes\r";exp_continue }
"id_rsa" { send "yes\r";exp_continue }
"*assword" { send "$passwd\r" ;exp_continue }
"test@" {
send "sudo su -\r"
expect {
"password for test" { send "$passwd\r";exp_continue }
}
}
}
}
expect eof
exit 0
3、批量定义inventory
直接利用for循环进行批量的写入
for i in $(seq 200 240);do echo 192.168.1.$i >> /etc/ansible/hosts;done
ansible 自动ssh的更多相关文章
- ansible不配ssh连接,用户密码登录
ansible 不配ssh免密链接,直接用ssh用户密码连接,要先装sshpass. 否则会报错: sshpass安装 sshpass下载地址:http://sourceforge.net/proje ...
- 更好的自动ssh登录
更好的自动ssh登录 解决~/.ssh/known_hosts 过期问题. bash + expect bash:ssh.sh #!/bin/bash help(){ echo "usage ...
- 基于docker的gitlab+gitlabrunner+ansible自动部署
系统架构图 网络架构 一.安装docker,确保hostname没有问题 ,查看/etc/hostname./etc/hosts. https://docs.docker.com/engine/ins ...
- 用Python写个自动ssh登录远程服务器的小工具
很多时候我们喜欢在自己电脑的终端直接ssh连接Linux服务器,而不喜欢使用那些有UI界面的工具区连接我们的服务器.可是在终端使用ssh我们每次都需要输入账号和密码,这也是一个烦恼,所以我们可以简单的 ...
- ansible的SSH连接问题
问题描述: 在ansible安装完毕后一般需要以SSH的方式连接到需要进行管理的目标主机,一开始遇到了如下问题: # ansible -m ping all 10.200.xx.xx | UNREAC ...
- 【Ansible】SSH Error: ssh_exchange_identification: Connection closed by remote host
ansible ssh到目标机器 时好时坏,报错: SSH Error: ssh_exchange_identification: Connection closed by remote host ...
- ansible 关闭ssh首次连接时提示
关闭ssh首次连接时提示. 修改/etc/ansible/ansible.cfg配置文件 方法一:(推荐,配置文件中存在) host_key_checking = False 方法二: ssh_arg ...
- ansible自动部署Keepalived实现Nginx服务双机热备
脚本实现通过ansible-playbook自动化安装Keepalived和配置,主要解决问题如下: Keepalived自动化安装: keepalived_vrid配置,自动根据vip获取最后一段作 ...
- ansible实现SSH配置免密互信
Ansible是用来处理大批量重复性操作的工具,只需要在一台机器上就可以远程控制所有机器,但前提是必须保证每台机器之间SSH可以相互免密登录.关于Ansible的安装和环境准备请参考Ansible环境 ...
随机推荐
- springboot 集成 freemarker
前面我们已经实现了thymeleaf模板,其实freemarker和thymeleaf差不多,都可以取代JSP页面,实现步骤也差不多,我们来简单实现一下 引入pom.xml依赖如下 <depen ...
- man
Description n间房子高度不同,Man 要从最矮的房子按照高度顺序跳到最高的房子,你知道房子的顺序,以及Man一次最远可以跳多远,相邻的房子至少有1的距离,房子的宽不计,现在由你安排相邻房子 ...
- Linux下批量替换文件内容和文件名(转)
1.批量替换指定多个文件的文件内容 在指定目录/your/path里,查找包含old_string字符串的所有文件,并用new_string字符串替换old_string字符串. sed -i &qu ...
- 2008 SQL SERVER 用户 架构
2008 SQL SERVER 用户: SERVER用户与数据库用户 SERVER 与 数据库用户的映射,以使 登陆用户可访问数据库 架构等同于SCHEM (表空间),即表空间管理对象,建立层次对象关 ...
- Linux BPF/bcc for Oracle Tracing
Luca Canali on 26 May 2016 Topic: In this post you will find a short discussion and pointers to the ...
- 和菜鸟们一起攻克金盾2018SS加密视频
今天刚拿到金盾2018SS加密的视频,界面如下:<ignore_js_op> 机器码由源来的“00000-00000-00000-00000”变成了一串32位的字符串了:<ignor ...
- update tableView contenSize
NSIndexPath *messageIndexPath = [NSIndexPath indexPathForRow:afterRowCount-1 inSection:0]; [self. ...
- windows平台是上的sublime编辑远程linux平台上的文件
sublime是个跨平台的强大的代码编辑工具,不多说. 想使用sublime完毕linux平台下django网站的代码编辑工作以提高效率(原来使用linux下的vim效率较低,适合编辑一些小脚本). ...
- Solaris Samba服务器与DNS服务
用于文件传输的协议,类似于ftp,ssh,只是它比其他两个好用. Samba协议 NetBIOS :一种编程接口. SMB:server message block .主要作为Microsoft网络通 ...
- python(24)- 面向对象进阶
面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个‘函数’供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板 ...