Macos(Big Sur 11.4)安装ansible以后,在执行指令时有如下提示:

~ sudo ansible all -m ping
192.168.100.180 | FAILED! => {
    "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

sshpass是ansible密码输入的必要条件,在Linux中使用yum install sshpass或者apt-get install sshpass都可以轻松安装,但在macOS新版本中由于安全原因无法直接使用brew install sshpass,需要采用其它安全的办法绕过。

在macOS下使用ansible命令(inventory文件中使用了密码验证的方式)或者使用iTerm2来完成自动密码填充等场景会使用到sshpass。

比如下面的样例:Inventory文件中使用了ansible_ssh_pass选项

编辑inventory

cat hosts
10.10.66.66 ansible_port=22 ansible_user=root ansible_ssh_pass=test666

使用ansible命令会失败,提示缺少sshpass

ansible all -i test.hosts -m ping
10.10.66.66 | FAILED! => {
"failed": true,
"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

安装sshpass及各种常见小问题处理

直接brew install会提示不安全,被拒绝,brew install --force强制安装也不行

brew install sshpass -f
Updating Homebrew...
Error: No available formula with the name "sshpass"
We won't add sshpass because it makes it too easy for novice SSH users to
ruin SSH's security.

可以通过下面的命令进行安装

brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

Error: Calling Non-checksummed download of sshpass formula file from an arbitrary URL is disabled! Use 'brew extract' or 'brew create' and 'brew tap-new' to create a formula file in a tap on GitHub instead.

# 出现上述报错可以下载至本地再次执行安装
wget https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
brew install sshpass.rb

sshpass.rb文件内容

require 'formula'

class Sshpass < Formula
  url 'http://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz'
  homepage 'http://sourceforge.net/projects/sshpass'
  sha256 'c6324fcee608b99a58f9870157dfa754837f8c48be3df0f5e2f3accf145dee60'

  def install
    system "./configure", "--disable-debug", "--disable-dependency-tracking",
                          "--prefix=#{prefix}"
    system "make install"
  end

  def test
    system "sshpass"
  end
end

如果(因为众所周知的网络原因)通过brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb的方式不能正常工作,可以将上面rubby脚本sshpass.rb的内容拷贝到本地执行 brew install sshpass.rb

最后修改:2021 年 06 月 21 日 04 : 15 PM
如果觉得我的文章对你有用,请随意赞赏