Ansible on macOS sshpass program workaround - macos

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>

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.

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

Running Ansible playbooks on remote Vagrant box

I have one machine (A) from which I run Ansible playbooks on a variety of hosts. Vagrant is not installed here.
I have another machine (B) with double the RAM that hosts my Vagrant boxes. Ansible is not installed here.
I want to use Ansible to act on Vagrant boxes the same way I do all other hosts; that is, running ansible-playbook on machineA while targeting a virtualized Vagrant box on machineB. SSH keys are already set up between the two.
This seems like a simple use case but I can't find it clearly explained anywhere given the encouraged use of Vagrant's built-in Ansible provisioner. Is it possible?
Perhaps some combination of SSH tunnels and port forwarding trickery?
Turns out this was surprisingly simple. Vagrant in fact does not need to know about Ansible at all.
Ansible inventory on machineA:
default ansible_host=machineB ansible_port=2222
Vagrantfile on machineB:
Vagrant.configure("2") do |config|
...
config.vm.network "forwarded_port", id: "ssh", guest: 22, host: 2222
...
end
The id: "ssh" is the important bit, as this overrides the default SSH behavior of restricting SSH to the guest from localhost only.
$ ansible --private-key=~/.ssh/vagrant-default -u vagrant -m ping default
default | SUCCESS => {
"changed": false,
"ping": "pong"
j }
(Note that the Vagrant private key must be copied over to the Ansible host and specified at the command line).

Access django server in vagrant virtualbox on host machine?

I am using windows and putty to ssh to vagrant virtualbox.I cannot access the django server running in vagrant virtualbox using http://localhost:9991
I have disabled my firewall as well
here's my vagrant file:
VAGRANTFILE_API_VERSION = "2"
def command?(name)
`which #{name}`
$?.success?
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# For LXC. VirtualBox hosts use a different box, described below.
config.vm.box = "fgrehm/trusty64-lxc"
# The Zulip development environment runs on 9991 on the guest.
config.vm.network "forwarded_port", guest: 9991, host: 9991, host_ip: "127.0.0.1"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.synced_folder ".", "/srv/zulip"
# Specify LXC provider before VirtualBox provider so it's preferred.
config.vm.provider "lxc" do |lxc|
if command? "lxc-ls"
LXC_VERSION = `lxc-ls --version`.strip unless defined? LXC_VERSION
if LXC_VERSION >= "1.1.0"
# Allow start without AppArmor, otherwise Box will not Start on Ubuntu 14.10
# see https://github.com/fgrehm/vagrant-lxc/issues/333
lxc.customize 'aa_allow_incomplete', 1
end
end
end
config.vm.provider "virtualbox" do |vb, override|
override.vm.box = "ubuntu/trusty64"
# 2GiB seemed reasonable here. The VM OOMs with only 1024MiB.
vb.memory = 2048
end
$provision_script = <<SCRIPT
set -x
set -e
sudo apt-get update
sudo apt-get install -y python-pbs
/usr/bin/python /srv/zulip/provision.py
SCRIPT
config.vm.provision "shell",
# We want provision.py to be run with the permissions of the vagrant user.
privileged: false,
inline: $provision_script
end
How do i access the server from host(Windows)?
I would suggest (on the Guest):
sudo netstat -lnutp
and having a look at what ports are open, and the process which owns them. If the one you want is missing, make sure the service which is responsible for it has been started, or start it yourself. From the looks of your Vagrantfile, this would be the "Zulip development environment".

Vagrant - PhpStorm - Laravel - HTTP Where can I access my local website?

I am new to to Php/Laravel and VMs world.
I booted up vagrant with this Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "laravel/homestead"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder "./devpeople", "/home/vagrant/devpeople"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
I've tried expirimenting using vagrant commands from their documentation, plus modifying the Vagrantfile without success.
What I want is a similar "site mapping" expirience, just like when you edit the Homestead.yaml file. Plus what is supposed to be the default way?
create a folder in your host machine something like
c:/projects/devpeople
Modify your Vagrant file like this
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 80, host: 8080
or you can change the port like this
config.vm.network "forwarded_port", guest: 80, host: 8081
see below
create a folder in your virtual machine
vagrant up
vagrant ssh
cd var/www
mkdir devpeople
So your virtual machine should have a folder like this
/var/www/devpeople
Then you can put your projects in your host machine
So the Vagrant sync folder will look like this
config.vm.synced_folder "c:/projects/devpeople", "/var/www/devpeople"
Run
vagrant up
and access your development site
localhost:8081
If the folder does not "sync", run
vagrant halt
to restart vagrant
then
vagrant up
You will need to forward ports from within your VM to the host. Here is an example from my Vagrantfile for Payara:
config.vm.network :forwarded_port, guest: 4848, host: 4849
config.vm.network :forwarded_port, guest: 8080, host: 8081
So because I know that Payara uses ports 4848 and 8080 by default, I have forwarded them to similar ports on my host (to avoid clashes).
So if I went to http://localhost:4849 after running vagrant up, I would be redirected to port 4848 within the VM, as though it was running locally.
Looking into the Laravel documentation, it looks like the following ports are the ones you need to make sure are available:
SSH: 2222 → Forwards To 22
HTTP: 8000 → Forwards To 80
HTTPS: 44300 → Forwards To 443
MySQL: 33060 → Forwards To 3306
Postgres: 54320 → Forwards To 5432
The docs imply that this is done by default, though, so you may want to try using them first to make sure.
Well I missed a vital step in the laravel documentation. The proper way to setup your vagrant box is by cloning the "setup" files from the
laravel/homestead repository.
So in order to access the server you simply edit the sites, on the Homstead.yaml file.

Vagrant Config Error - "A box must be specified."

The boxes were working fine. Then I halted one (the only one running at the time) and now I can't get either of them back up.
Running vagrant up [name] gives me the following error, regardless of which I pick or whether I leave it at vagrant up for them both to come up:
There are errors in the configuration of this machine. Please fix
the following errors and try again:
vm:
* A box must be specified.
Running latest version of Vagrant (1.7.4).
Here is my Vagrantfile in its entirety, comments included (just in case):
# Search for boxes here: https://atlas.hashicorp.com/boxes/search
# Refer to commands_vagrant.txt for command reference
Vagrant.configure("2") do |config|
# Globally defined variables
config.vm.synced_folder "./", "/var/www/public"
# CentOS 6.5, Apache 2.2.15, MySQL 5.5.36 (-u root), PHP 5.3.28
# Note: If PHP session keys don't work, set permissions to 777 (or other more restrictive, but this is guaranteed to work) on /var/lib/php/session
config.vm.define "php5dot3", primary: true do |php5dot3|
config.vm.box = "smallhadroncollider/centos-6.5-lamp"
config.vm.network :forwarded_port, guest: 80, host: 4567
end
# Ubuntu 14.04 (SSH pw: vagrant), Apache 2.4.12, MySQL 5.5.43 (-u root -p root), PHP 5.6.10
config.vm.define "php5dot6" do |php5dot6|
config.vm.box = "scotch/box"
config.vm.network :forwarded_port, guest: 80, host: 4568
end
end
Result of running vagrant status:
Current machine states:
php5dot3 poweroff (virtualbox)
php5dot6 poweroff (virtualbox)
Result of running vagrant global-status:
id name provider state directory
--------------------------------------------------------------------------
e1f3c85 default virtualbox poweroff /home/sam/Web
c588d51 php5dot6 virtualbox poweroff /home/sam/Web
4e71c50 php5dot3 virtualbox poweroff /home/sam/Web
'default' was the singular box I had in my Vagrantfile before I got multi-machines working last week. (Relevant?)
Result of running vagrant box list:
scotch/box (virtualbox, 2.0)
smallhadroncollider/centos-6.5-lamp (virtualbox, 1.0.0)
Any help would be appreciated, thanks.
Inside of your machine definitions, you need to use the variable name of that machine, instead of config. Try this out:
In the file below, I've changed config.vm to either php5dot3.vm or php5dot6.vm:
Vagrant.configure("2") do |config|
# Globally defined variables
config.vm.synced_folder "./", "/var/www/public"
# CentOS 6.5, Apache 2.2.15, MySQL 5.5.36 (-u root), PHP 5.3.28
# Note: If PHP session keys don't work, set permissions to 777 (or other more restrictive, but this is guaranteed to work) on /var/lib/php/session
config.vm.define "php5dot3", primary: true do |php5dot3|
php5dot3.vm.box = "smallhadroncollider/centos-6.5-lamp"
php5dot3.vm.network :forwarded_port, guest: 80, host: 4567
end
# Ubuntu 14.04 (SSH pw: vagrant), Apache 2.4.12, MySQL 5.5.43 (-u root -p root), PHP 5.6.10
config.vm.define "php5dot6", autostart:false do |php5dot6|
php5dot6.vm.box = "scotch/box"
php5dot6.vm.network :forwarded_port, guest: 80, host: 4568
end
end
I also added autostart:false to the definition of your php5dot6 box, which you can remove if you wish. (It just means that running vagrant up will only start the primary by default.
If you are getting this error with DigitalOcean, you may need their plugin:
vagrant plugin install vagrant-digitalocean
Installing the 'vagrant-digitalocean' plugin. This can take a few minutes...
Fetching: multipart-post-2.0.0.gem (100%)
Fetching: faraday-0.15.4.gem (100%)
Fetching: vagrant-digitalocean-0.9.3.gem (100%)
For someone that is having this issue now:
I had deleted my Vagrantfile before trying to destroy it. You need to run the vagrant destroy command from the right directory where the Vagrantfile for that process is.
Run vagrant ssh-config and look at the directory column.
If you, like me, deleted the file, do:
vagrant init
Then
vagrant destroy $id
P.S.: Use sudo if you have permission issues running those commands.

Forward Ports from boot2docker using the Vagrant Docker provider

I'm trying to utilize Vagrant 1.6's Docker provider and I seem to have run into a snag. I can successfully bring up the Docker container and guest OS, but then I can't access the service I've brought up within the container from the host OS. Here's my Vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.network :forwarded_port, guest: 8000, host: 8000
config.vm.define "icecast" do |v|
v.vm.provider "docker" do |d|
d.image = "moul/icecast"
d.ports = ["8000:8000"]
d.env = {
# SOURCE_PASSWORD: 'password',
ADMIN_PASSWORD: 'password',
# PASSWORD: 'password',
# RELAY_PASSWORD: 'password'
}
end
end
end
My understanding is that running vagrant up --provider=docker on OS X will start a VM running boot2docker that will then run my container. Running vagrant docker-logs seems to confirm that my container is created and the service started, but now I can't for the life of me figure out how to access the service from my OS X host. If I was using a standard VirtualBox provider, I would expect the config.vm.network :forwarded_port directive to handle the forwarding, but adding that doesn't seem to make any difference.
What do I need to do to be able to access this service from my OS X host?
Update: For reference, here is the image's Dockerfile: https://github.com/moul/docker-icecast/blob/master/Dockerfile
Ok, so I finally figured this out and it turns out the solution is to not use boot2docker at all. Based on some diving I did through the Vagrant source, reading issues, and rewatching the Docker provider introduction videos, it turns out that you need to use a proxy VM to host your containers instead of boot2docker.
To set this up, I modified my Vagrantfile to include a configuration option for vagrant_vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "icecast" do |v|
v.vm.provider "docker" do |d|
d.image = "moul/icecast"
d.ports = ["8000:8000"]
d.env = {
# SOURCE_PASSWORD: 'password',
ADMIN_PASSWORD: 'password',
# PASSWORD: 'password',
# RELAY_PASSWORD: 'password'
}
d.vagrant_vagrantfile = "./Vagrantfile.proxy"
end
end
end
Then I added an additional file (Vagrantfile.proxy) that Vagrant will use to spin up the proxy VM:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provision "docker"
config.vm.provision "shell", inline:
"ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
config.vm.network :forwarded_port, guest: 8000, host: 8000
end
Using the Docker provisioner will automatically install Docker on the proxy VM for you. The inline shell script forces Vagrant to log back into the box so that it can utilize Docker after it's been installed. Finally, I forward the port I need in this Vagrantfile as opposed to the original (while still using the ports config option in the original).
Just like with the default boot2docker strategy, Vagrant will be smart enough to reuse existing instances of the proxy VM for any image that utilizes it.
Hopefully this will be helpful to someone down the road.
To forward Ports from boot2docker (as opposed to forwarding ports from a custom proxy VM that's not using boot2docker), you need to add port forwards manually through VirtualBox, or run the following script after running vagrant up:
export PORT=3306
export REASON=mysql
export HOST_VM=`VBoxManage list runningvms | grep docker-host | awk '{ print $1 }' | sed 's/"//g'`
VBoxManage controlvm $HOST_VM natpf1 "docker-$REASON-$PORT-port-forward,tcp,127.0.0.1,$PORT,,$PORT"

Resources