Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Installing and running Ansible Local via Vagrant

I am using Vagrant and trying to enable Ansible to work with it. Because I am working in a virtualenv with Python 3.5.0, I have to use Ansible-Local – the "main" Ansible will not run on my host because I am not running Python 2.x.

Regardless, everything mostly works, except that vagrant up does not find Ansible. Here is the output from the call:

==> default: Running provisioner: ansible_local...
    default: Installing Ansible...
The Ansible software could not be found! Please verify
that Ansible is correctly installed on your guest system.

If you haven't installed Ansible yet, please install Ansible
on your Vagrant basebox, or enable the automated setup with the
`install` option of this provisioner. Please check
https://docs.vagrantup.com/v2/provisioning/ansible_local.html
for more information.

However, if I use vagrant ssh to access the VM, Ansible is clearly installed:

vagrant@vagrant-ubuntu-trusty-64:~$ ansible --version
ansible 2.0.0.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

I'm at a loss of what to do. I can run ansible-playbook server.yml, while SSH'd into the server, and it works correctly. However, the whole point of me using Ansible is that I could automatically run it via Vagrant. Here's the relevant portion of my Vagrantfile:

  config.vm.provision "ansible_local" do |ansible|
      ansible.install = true
      ansible.version = "latest"
      ansible.sudo = true
      ansible.playbook = "server.yml"
  end

Any suggestions are greatly appreciated. Thank you!

like image 860
Rushy Panchal Avatar asked Feb 09 '16 18:02

Rushy Panchal


People also ask

How does the Ansible local Provisioner work?

»Ansible Local Provisioner. The Vagrant Ansible Local provisioner allows you to provision the guest using Ansible playbooks by executing ansible-playbook directly on the guest machine. Warning: If you are not familiar with Ansible and Vagrant already, we recommend starting with the shell provisioner.

What is the purpose of the command Vagrant up in Ansible?

Vagrant is a tool to manage virtual machine environments, and allows you to configure and use reproducible work environments on top of various virtualization and cloud platforms. It also has integration with Ansible as a provisioner for these virtual machines, and the two tools work together well.


1 Answers

By bringing the box up in debug mode:

VAGRANT_LOG=debug vagrant up

Shows:

DEBUG ssh: Re-using SSH connection.
 INFO ssh: Execute: ansible-galaxy --help && ansible-playbook --help (sudo=false)
DEBUG ssh: stderr: ERROR! Missing required action

DEBUG ssh: stdout: Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...

Options:
  -h, --help     show this help message and exit
  -v, --verbose  verbose mode (-vvv for more, -vvvv to enable connection
                 debugging)
  --version      show program's version number and exit

DEBUG ssh: Exit status: 5

ansible-galaxy --help is the problem as it expected an action. Because that failed, vagrant thinks that there was a problem with the install.

It looks like this has been fixed on the master branch and will be in the next version.

What you could try is:

# -*- mode: ruby -*-
# vi: set ft=ruby :

$install_ansible = <<SCRIPT
apt-get -y install software-properties-common
apt-add-repository ppa:ansible/ansible
apt-get -y update
apt-get -y install ansible

SCRIPT

Vagrant.configure(2) do |config|
  config.vm.box = 'ubuntu/trusty64'
  config.vm.provision 'shell', inline: $install_ansible
  # Patch for https://github.com/mitchellh/vagrant/issues/6793
  config.vm.provision "shell" do |s|
    s.inline = '[[ ! -f $1 ]] || grep -F -q "$2" $1 || sed -i "/__main__/a \\    $2" $1'
    s.args = ['/usr/bin/ansible-galaxy', "if sys.argv == ['/usr/bin/ansible-galaxy', '--help']: sys.argv.insert(1, 'info')"]
  end
  config.vm.provision :ansible_local do |ansible|
    ansible.sudo = true
    ansible.playbook = 'server.yml'
  end
end

note: Credit to MasonM for the following snippet:

# Patch for https://github.com/mitchellh/vagrant/issues/6793
config.vm.provision "shell" do |s|
    s.inline = '[[ ! -f $1 ]] || grep -F -q "$2" $1 || sed -i "/__main__/a \\    $2" $1'
    s.args = ['/usr/bin/ansible-galaxy', "if sys.argv == ['/usr/bin/ansible-galaxy', '--help']: sys.argv.insert(1, 'info')"]
end 

Or another option while waiting for the fix to be released is building vagrant from source.

like image 70
jaxim Avatar answered Nov 14 '22 13:11

jaxim