【问题标题】:Issue remoting into a device and doing a simple ping test with Ansible问题远程处理到设备并使用 Ansible 进行简单的 ping 测试
【发布时间】:2016-02-10 23:19:24
【问题描述】:

按照在线和几本书中的说明进行操作后,我不确定为什么会发生这种情况。我感觉缺少设置,但设置如下:

我正在尝试使用命令:

ansible all -u $USER -m ping  -vvvv

显然使用 -vvvv 进行调试,但除了它说它正在尝试连接之外没有太多输出。我收到以下错误:

S4 | FAILED => FAILED: Authentication failed.

S4 代表交换机 4,我正在尝试自动配置并在其上显示命令的 Cisco 交换机。我知道我在 host_vars 文件中设置的密码 100% 是正确的,因为它在我从标准 SSH 客户端使用它时有效。

这是我在 ansible.cfg 文件中的非默认配置设置:

[defaults]
transport=paramiko
hostfile = ./myhosts
host_key_checking=False
timeout = 5

我的 myhosts 文件:

[cisco-switches]
S4

还有我的 S4 的 host_vars 文件:

ansible_ssh_host: 192.168.1.12
ansible_ssh_pass: password

我当前的版本是 1.9.1,在 Centos 虚拟机上运行。我确实在交换机的管理接口上应用了 ACL,但它允许来自该特定 IP 的远程连接。

请指教。

【问题讨论】:

  • 你能用-vvv 运行命令来得到一些详细的错误信息吗?
  • 输出与我上面指定的 -vvvv 选项相同,但这里是:<192.168.1.11> ESTABLISH CONNECTION FOR USER: username on PORT 22 TO 192.168.1.11 S4 | FAILED => FAILED: Authentication failed.
  • 那么就是它所说的,您不能使用该用户名和密码组合通过端口 22 从该计算机通过 SSH 连接到该交换机。
  • 问题是,当我在标准 bash 上使用 ssh username@192.168.1.11 命令时,我可以毫无问题地访问我的设备。所以很明显我能够做到。我感觉这与 rsa 密钥设置有关。

标签: automation ansible cisco


【解决方案1】:

由于您使用 ansible 在 Cisco 交换机中自动执行命令,我猜您希望在不提示输入密码或被要求按 [Y/N] 确认连接的情况下执行与交换机的 SSH 连接。

为此,我建议在交换机上配置 Cisco IOS SSH 服务器以执行基于 RSA 的用户身份验证。

首先你需要在你的 Linux 机器上生成 RSA 密钥对:

ssh-keygen -t rsa -b 1024

注意:您可以使用 2048 代替 1024,但考虑到某些 IOS 版本将接受最多 254 个字符的 ssh 公钥。

在开关侧:

conf t    
ip ssh pubkey-chain
     username test
          key-string
               Copy the entire public key as appears in the cat id_rsa.pub
               including the ssh-rsa and username@hostname.
               Please note that some IOS versions will accept 
               maximum 254 characters.
               You can paste multiple lines.     
          exit
     exit

如果您需要“测试”用户可以执行特权 IOS 命令:

username test privilege 15 secret _TEXT_CLEAR_PASSWORD_

然后,从您的 Linux 机器测试您的连接,以便将开关添加到 known_hosts 文件。对于在 known_hosts 文件中找不到的每个交换机/主机,这只会发生一次:

ssh test@10.0.0.1
The authenticity of host '10.0.0.1 (10.0.0.1)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:d6:4b:d1:67.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.1' (RSA) to the list of known hosts.

ciscoswitch#
ciscoswitch#exit

最后使用 ansible over SSH 和 raw 模块测试连接,例如:

ansible inventory -m raw -a "show env all" -u test

希望对你有用。

【讨论】:

  • 太棒了!谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
相关资源
最近更新 更多