第一个Ansible测试case,ansible测试case


安装Ansible

brew install ansible

创建自己的playbooks

cd /Users/wangzhen/works/oneDriver/OneDrive/Developer
mkdir playbooks

## 创建hosts
在playbooks目录下创建一个名叫hosts的文件,这个文件将作为inventory文件。在hosts文件中写入下面内容:

testserver ansible_ssh_host=192.168.3.175 ansible_ssh_port=22 ansible_ssh_user=root

验证ansible连接testserver

让我们告诉Ansible连接到名为testserver的服务器(在inventory的hosts文件中描述并引入ping模块)

LS-MacBook-Pro:playbooks wangzhen$ ansible testserver -i hosts -m ping 
The authenticity of host '192.168.3.175 (192.168.3.175)' can't be established.
RSA key fingerprint is SHA256:gzfyP+1OOYtxE+/fwFxzk+QiWe/28zSigo96sepjmGM.
Are you sure you want to continue connecting (yes/no)? yes
testserver | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.3.175' (RSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}

此问题的处理方法

很明显是因为没有远程服务器的秘钥导致。
1、在macbook本地生成rsa公钥

localhost:.ssh wangzhen$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wangzhen/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/wangzhen/.ssh/id_rsa.
Your public key has been saved in /Users/wangzhen/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:wQ0bsfp182PpXdY92wehXkGnVnMmrwT9sovmwd/U2zQ wangzhen@localhost
The key’s randomart image is:
+—[RSA 2048]—-+
| +. . |
| . * . +.=|
| = . o Oo|
| . . B o|
| . S . o+ * |
| . . o.o=.+|
| . .oo=EO|
| +=.BX|
| o. oo*|
+—-[SHA256]—–+

2、将此公钥内容拷贝到被管理linux服务器的authorized_keys文件中

[root@linux6 .ssh]# vi authorized_keys
[root@linux6 .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMMePzV7ZRSiKId5z/oA9cAw2MBvXGTL6Pu7ZVcb3TYrRL2NRIfireXWZimWwwWFmfCpd6Xx5G9PyhQXsaV5bw+RwgasyChEoTo6NOpqKlvjoV6NefLhB4r2x0z/T4KCm+NTZd5aUqFGhJbzj+mIBV9XeeGM4rhASoUCeHNS2MeYtBi/df32KRwlTaXkeWOYUOIE3jnHC4+UmZbDSNgo+2BkNN1CmuxHCBF3LHYmjCwnKx4F4CZ5Ohl3haVXUZ8oFcQxSaAzfwr4wMJcNR3A/d6Mojric5tIH97tEwv1gbGzo0xGJLJXdHxS7QCJiBeFkltF3oPHC8Y7O+Ge1N+KbH wangzhen@localhost

再次执行ansible测试,连接成功

LS-MacBook-Pro:playbooks wangzhen$ ansible testserver -i hosts -u root -m ping
testserver | SUCCESS => {
“changed”: false,
“ping”: “pong”
}

相关内容

    暂无相关文章