Home > other >  Ansible on macOS sshpass program workaround
Ansible on macOS sshpass program workaround

Time:12-06

I'm using homebrew to install Ansible on macOS Catalina (I previously installed via pip too per the documentation). The problem is that when I attempt to use a test playbook, I receive the following error:

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

The issue is that sshpass isn't readily available on macOS via homebrew, etc. I've found a couple of options of installation for this but attempted to make the following changes prior to installing this:

export ANSIBLE_HOST_KEY_CHECKING=False

host_key_checking=false within the ansible.cfg in the same directory

None of the above changes worked, should I just install sshpass, or is there another workaround? Or should I just use virtualbox and call it a day?

For reference, this is the following playbook, it's a simple ping test that I'm attempting to use on a local Raspberry Pi that I've already been able to SSH into:

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

The inventory.txt file looks like this:

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

CodePudding user response:

Should I just install sshpass, or is there another workaround? Or should I just use virtualbox and call it a day?

It depends on the use case. What do you want to do? Use Ansible for development purposes, or use the machine with IP 192.168.x.x for production workloads?

It is preferred to use ssh keypairs instead of passwords. You can create these on the target host by executing command: "ssh-keygen". This way, you can 'work-around' the use of sshpass.

To help you out with using Virtualbox/Vagrant. After installing Vagrant, create a file named "Vagrantfile" in a directory, place this in there:

# -*- 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

Place this in /etc/vbox/networks.conf. This allows the usage of the 10.x.x.x network in Vagrant.

* 10.0.0.0/8 192.168.56.0/21

Create an inventory file, named 'inventory', and place this content in there. Replace my_username with your 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

Then, create an Ansible playbook like this:

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

Now, you can execute command: "vagrant up", and the VM will be automatically created, and the playbook will be executed automatically as well.

CodePudding user response:

This ended up being more of a novice issue as I am still very new to the tool. Within my inventory file, I added ansible_user=pi which resolved the issue here.

To solve this, I logged into the raspberry pi via a manual ssh connection and ran the command systemctl status sshd. This showed me multiple login failures and that ansible was defaulting to my macOS user.

  • Related