【问题标题】:Ansible on macOS sshpass program workaroundAnsible on macOS sshpass 程序解决方法
【发布时间】:2022-01-08 16:03:42
【问题描述】:

我正在使用自制软件在 macOS Catalina 上安装 Ansible(我之前也根据文档通过 pip 安装过)。问题是当我尝试使用测试剧本时,我收到以下错误:

target1 | FAILED! => {
    "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

问题是 sshpass 在 macOS 上无法通过 homebrew 等轻松获得。我找到了几个安装选项,但在安装之前尝试进行以下更改:

export ANSIBLE_HOST_KEY_CHECKING=False

host_key_checking=falseansible.cfg同目录

上述更改均无效,我应该只安装 sshpass,还是有其他解决方法?还是我应该只使用 virtualbox 并收工?

作为参考,这是下面的剧本,这是一个简单的 ping 测试,我试图在我已经能够通过 SSH 连接到的本地 Raspberry Pi 上使用它:

-
  name: Test connectivity to target servers
  hosts: all
  tasks:
    - name: Ping test
      ping:

inventory.txt 文件如下所示:

target1 ansible_host=192.168.x.x ansible_ssh_pass=<password>

【问题讨论】:

    标签: macos ansible ansible-inventory sshpass


    【解决方案1】:

    我应该只安装 sshpass,还是有其他解决方法?还是我应该只使用 virtualbox 并收工?

    这取决于用例。你想让我做什么?将 Ansible 用于开发目的,还是将 IP 192.168.x.x 的机器用于生产工作负载?

    最好使用 ssh 密钥对而不是密码。您可以通过执行命令在目标主机上创建它们:“ssh-keygen”。这样,您可以“变通”使用 sshpass。

    帮助您使用 Virtualbox/Vagrant。 安装 Vagrant 后,在目录中创建一个名为“Vagrantfile”的文件,将其放在那里:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    Vagrant.configure("2") do |config|
    
      config.vm.provider "virtualbox" do |v|
        v.memory = 2048
        v.cpus = 2
      end
    
    config.ssh.insert_key = false
    
    config.vm.define "vm-local-1" do | me |
      me.vm.box = "rocky8-python3"
      me.vm.hostname = "vm-local-1"
      me.vm.network :forwarded_port, guest: 22, host: 65530, id: "ssh"
      me.vm.network :forwarded_port, guest: 80, host: 8080
      me.vm.network :forwarded_port, guest: 443, host: 4443
        me.vm.network :forwarded_port, guest: 27017, host: 27017
        me.vm.network "private_network", ip: "10.0.0.110"
        me.vm.provision "ansible" do |ansible|
          ansible.playbook = "playbook.yml"
          ansible.inventory_path = "inventory"
          ansible.limit = "vm-local-1"
          end
        end
    end
    

    把它放在/etc/vbox/networks.conf。这允许在 Vagrant 中使用 10.x.x.x 网络。

    * 10.0.0.0/8 192.168.56.0/21
    

    创建一个名为“inventory”的库存文件,并将此内容放入其中。将 my_username 替换为您的用户名。

    [local_test]
    vm-local-1 ansible_ssh_user=vagrant ansible_host=127.0.0.1 ansible_ssh_port=65530 ansible_ssh_private_key_file=/Users/<my_username>/.vagrant.d/insecure_private_key
    
    [local_test:vars]
    ansible_python_interpreter=/usr/bin/python3
    

    然后,像这样创建一个 Ansible 剧本:

    ---
    - hosts: local_test
      gather_facts: false
      become: true
      tasks:
         - shell: echo
    

    现在,你可以执行命令:“vagrant up”,虚拟机会自动创建,playbook也会自动执行。

    【讨论】:

    • 谢谢!由于我是该工具的新手,因此这最终成为一个新手 ansible 问题。
    【解决方案2】:

    这最终成为一个新手问题,因为我对该工具仍然很陌生。在我的inventory 文件中,我添加了ansible_user=pi,这解决了这里的问题。

    为了解决这个问题,我通过手动 ssh 连接登录到树莓派并运行命令systemctl status sshd。这显示了我多次登录失败,并且 ansible 默认为我的 macOS 用户。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2017-02-17
      • 2011-03-04
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      相关资源
      最近更新 更多