vagrant icon indicating copy to clipboard operation
vagrant copied to clipboard

Support Ansible “Local Playbooks”

Open mxey opened this issue 13 years ago • 43 comments

At the moment, the Ansible provisioner only supports running Ansible running over SSH. IMHO, this complicates the setup and requires Ansible on the host system (unlike the Puppet provisioner, for example).

Ansible supports running playbooks on localhost: http://www.ansibleworks.com/docs/playbooks2.html#id20

mxey avatar Aug 31 '13 17:08 mxey

/cc @commandtab

mitchellh avatar Sep 01 '13 04:09 mitchellh

+1 for this

avtar avatar Sep 13 '13 16:09 avtar

Fixing this would allow running vagrant with ansible on Windows (#1946) which is always useful for cross-platform collaboration. It's an issue for us as we're collaborating with a freelancer on a project and we can't get him set up yet. I'm trying to think about a workaround using the shell provisioner, but it's a bit irritating.

For the feature, I think it would be nice if it were switchable, like the multiple modes of Puppet and Chef.

alinnow avatar Oct 16 '13 14:10 alinnow

I am working on a plugin for this to execute playbook directly in the vm with --connection=local

jaugustin avatar Nov 01 '13 13:11 jaugustin

I start writing the plugin vagrant-ansible-local base on shell / puppet plugin, you could give it a try https://github.com/jaugustin/vagrant-ansible-local

jaugustin avatar Nov 03 '13 18:11 jaugustin

I agree that this feature request makes a lot of sense!

@jaugustin are you interested to work on adding this functionality to the main Ansible provisioner?

gildegoma avatar Nov 29 '13 23:11 gildegoma

@gildegoma If someone askme I can work on it :)

jaugustin avatar Nov 30 '13 14:11 jaugustin

In my opinion, pull request for this should absolutely be accepted. As already said above, local transport mode perfectly suits to Vagrant design.

@mitchellh confirm? First, it would be great to plot the expected architecture for such "multiple transport" support. Below a few thoughts... Many thanks in advance for your review.

Considering that many ssh features do not apply to local transport mode (and vice versa), I think that we could have two separated provisioners (e.g. ansible_local and ansible_ssh [maybe aliased as ansible for backward compatibility]), with a shared code base (similarly to chef_client and chef_solo implementation). This way each Ansible provisioner will provide only the relevant set of parameters, and thus should be easier to learn, use and maintain. But this is my first glance and a "monolithic" provisioner is certainly a possible variant worth to be considered...

Nice to have: optionally bootstrap Ansible installation on the target VM. Though this one could be done independently, maybe later in a following iteration.

gildegoma avatar Dec 11 '13 22:12 gildegoma

:+1: Specially on windows, which ansible does not support as a control machine.

cyrusdavid avatar Dec 13 '13 02:12 cyrusdavid

Yes, this sounds great. :)

mitchellh avatar Dec 31 '13 16:12 mitchellh

FYI: I've started to work on this feature request and plan to a open pull request in the next weeks...

About the design: it will be only one Ansible provisioner, with two possible ansible.mode:

  • run Ansible on Vagrant's :host (by default)
  • run Ansible on Vagrant's :guest machine(s)

gildegoma avatar Feb 24 '14 06:02 gildegoma

I've started to work on this feature request and plan to a open pull request in the next weeks...

Sorry for the delay, but I first want to introduce the specs (#3287). It still on my roadmap though!

gildegoma avatar Apr 04 '14 08:04 gildegoma

I've been using https://github.com/geerlingguy/JJG-Ansible-Windows for running playbooks from within Windows machines, and it seems to work well for either Cygwin or PowerShell-based installs. The only thing that doesn't seem to work that great is terminal colors coming through Cygwin... but that's a small thing.

I'd love to see official support, though!

geerlingguy avatar Apr 08 '14 16:04 geerlingguy

Huge :+1: for this ! And packer is doing the local provisioning with ansible.

afterdesign avatar Jun 20 '14 10:06 afterdesign

+1

krzysztof-indyk avatar Sep 08 '14 16:09 krzysztof-indyk

Any update on this?

mpolden avatar Sep 08 '14 16:09 mpolden

My work in progress is available at https://github.com/gildegoma/vagrant/commits/ansible-on-guest (be aware that this branch might be rebased in the future to be kept "merge compatible" with upstream master). In the current stage, the local (or guest-based) provisioning already works fine, but code, unit tests and docs are unfortunately not enough mature to be proposed as pull request yet.

Before starting to "finalize" this (hum, see all the trailing TODOs), I first need to validate some central points, and be sure that the current direction is good:

  • Agree on the new code organization (base, host, guest parts, better ruby style, etc)
  • Take naming decisions
    • For instance, do we agree on 'ansible-local' like in packer? :ansible_local would better follow existing :chef_solo or :puppet_server provisioners. I am afraid of confusion with "local", why not something like :ansible_on_guest ?)
  • Decide on documentation organization (a single page versus dedicated pages for both ansible provisioners). Even if common options could be shared via a third page (like for Chef Solo and Chef Client), I like the idea to keep it simple with a single page... but as a downside, both Ansible modes won't stand out from titles in the left menu.
  • Agree on the level of regression testing required by this "significant" refactoring (the host-based ansible provisioner should behave exactly the same). I have been using this code base on a regular basis for some time now, but later it would be great if some of you could test it before we officially ship it in a Vagrant release.

I am sorry, but at the moment I am lacking of time to further work on all of this, and for that reason I could not ping @mitchellh to make this "intermediate" review. I'll keep you informed, but if someone is interested in helping to accelerate this development, please announce you! You're welcome and I'd be happy to pair/delegate so we can get it in "sooner"!

And as always, :heart: to your thoughts and feedbacks!


For people interested to give a try to the current code, something like that can be used:

Vagrant.configure("2") do |config|
  config.vm.define 'machine1' do |machine|
    machine.vm.box = 'ubuntu/trusty64'
    machine.vm.hostname = 'machine1'

    # Install ansible on the target VM
    machine.vm.provision :shell, :inline => <<EOS
set -e
if ! command -V ansible-playbook >/dev/null 2>/dev/null; then
  sudo apt-get update -qq
  sudo apt-get install -qq ansible
fi
EOS

    # run ansible from the guest VM
    machine.vm.provision 'ansible-local' do |ansible|
      ansible.playbook = "your-playbook.yml"
    end
  end
end

gildegoma avatar Sep 09 '14 05:09 gildegoma

@gildegoma awesome work ! I would help but ruby is not on my "known languages" list right now.

afterdesign avatar Sep 10 '14 06:09 afterdesign

Would love to see this integrated. :+1:

magnetik avatar Sep 11 '14 12:09 magnetik

Vagrantfile


  # Install ansible at remote host.
  config.vm.provision "shell", path: "provisioning/init.sh", keep_color: true
  # Copy ansible playbook to guest machine
  config.vm.provision "file", source: "provisioning/guest/inventory", destination: "/home/vagrant/inventory"
  config.vm.provision "file", source: "provisioning/guest/playbook.yml", destination: "/home/vagrant/playbook.yml"
  # Run it!!!
  config.vm.provision "shell",
    path: "provisioning/ansible.sh",
    keep_color: true

init.sh

#!/bin/sh
sudo apt-get -yq install software-properties-common
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get -yq update
sudo apt-get -yq install ansible
sudo apt-get -yq install python-mysqldb
sudo apt-get -yq install libnotify-bin

ansible.sh

#!/bin/sh
sudo ansible-playbook -i /home/vagrant/inventory /home/vagrant/playbook.yml

playbook.yml

---
# This playbook will be executed at guest host.
- hosts: 127.0.0.1
  connection: local
  gather_facts: no

  tasks:
  -name: ...

The only issue is ansible output has no echo till the end of full process.

https://github.com/podarok/vansible

podarok avatar Oct 01 '14 14:10 podarok