最近在作自动化部署发布.读了一下ansible的代码和工作流.
需要部署一些免密码登录的操作. 记录下
现在有两台机器, 一台服务器A安装了ansible, 一台是服务器B需要被操作的.
随便建立一个项目文件夹.
为了方便管理, 我这样划分的项目
在conf里面放所有的配置, 包括hosts和ansible.cfg, 然后作一个软链接到最外面. ansible.cfg的优先级将是当前目录最高
a
在ssh_keys里面, 存放着很多服务器的私钥. 注意是私钥.
ansible用了paramiko库.
ansible.cfg 配置如下. 注意ssh_connection下面的contrl_path=./ssh_keys, 不指定的话会一直报group-readable or world-readable and thus insecure
说明:在hadoop集群配置过程中需要配置ssh无密码登录,这一步不是必须的,有很多人都误为认为必须要配置ssh无密码登录,这种理解是不正确的,但为什么平
[defaults]
inventory = ./conf/hosts
pull_interval = 15
sudo_user = root
transport = paramiko
module_lang = C
host_key_checking = False
sudo_exe = sudo
# SSH timeout
timeout = 30
#remote_user = root
#remote_port = 22
remote_tmp = $HOME/.ansible/tmp
[ssh_connection]
# if True, make ansible use scp if the connection type is ssh, default is sftp
scp_if_ssh = True
#sftp_batch_mode= False
control_path = ./ssh_keys
去服务器B上生成密钥对,
#ssh root@服务器B
#ssh-keygen 如果要重命名可以自己指定, 回车后生成密钥对
# echo ~/.ssh/id_rsa.pub >> known_hosts 这里把生成的公钥放入known_hosts
如果自己配置了ssh_config, 关闭了known_hosts, 可能就需要写进~/.authorized_keys里面去了.
把服务器B上的私钥回传到ansible执行的服务器A的ssh_keys里, 命名为
服务器B_ip_.key
给ssh_keys文件夹授权为700, 一定要700, 其它的都会报错.
建立hosts文件里面指定server, 每个server一行.这里我测试就写一行.
[test_server]
10.0.1.5 ansible_ssh_private_key_file=ssh_keys/10.0.1.5.key ansible_ssh_user=root
现在回到服务器A的这个ansible文件夹下测试:
ansible test_server -m shell -a ‘ls -la /etc/hostname’
10.0.1.5 | success | rc=0 >>
-rw-r--r-- 1 root root 15 Jun 4 2012 /etc/hostname
无密码成功.
一、安装ssh ubuntu中使用命令: $sudo apt-get install openssh-server openssh-client centos中使用命令: $sudo yum install openssh-server openssh-client
最近在作自动化部署发布.读了一下ansible的代码和工作流. 需要部署一些免密码登录的操作. 记录下 现在有两台机器, 一台服务器A安装了ansible, 一台是服务器B需要被操作的. 随便建立一个项目文件