1.查询ansible是否安装
rpm -ql ansible

若是安装了则先rpm -e ansible
2.配置管理主机免密其他主机
[root@zb-yunwei-test-203-51 .ssh]# ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:lSG4Eqc+u25ne3L+ACaMyR6YYoSKTlgAl3BhG286jQ0 root@zb-yunwei-test-203-51
The key's randomart image is:
+---[RSA 2048]----+
|=.*o .. . |
|.=.+. o . o |
|..E o+ . o |
|== @o . . |
|BoO.=.o S |
|=. ooo . |
| .. o . |
| o + o. |
| oo+.*... |
+----[SHA256]-----+
3.把密钥传给被管理的主机
vim ansible.sh
#!/bin/bash
for i in {52..53}
do
ssh-copy id 172.21.203.$i
done
./ansible.sh
4.配置ansible的hosts文件
vim /etc/ansible/hosts
[web]
zb-yunwei-test-203-52
zb-yunwei-test-203-53
可以定义不同的组,组内可多添加主机(前提是管理主机能免密登录)
5.验证ansible是否能应用
[root@zb-yunwei-test-203-51 .ssh]# ansible web -m ping
zb-yunwei-test-203-52 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
zb-yunwei-test-203-53 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@zb-yunwei-test-203-51 .ssh]#
6.可能遇到的问题
.ssh权限默认为700
authorized_keys的权限是600
有些时候ssh的配置文件不对,不允许免密登录,就需要进入/etc/ssh下,修改sshd_config文件里的参数。
HostKey /etc/ssh/ssh_host_rsa_key
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
配置完成免密登录就默认可以ansible
678

被折叠的 条评论
为什么被折叠?



