Setting up a Ubuntu VM with Vagrant and Ansible on Windows

As Ansible has no out-of-the-box support for running on Windows, there’s some tweaking to do.

Please note that these instructions just run Ansible against a vagrant managed VM, no Vagrant Ansible provisioning is involved.

You’ll install cygwin (+ some packages for it), Vagrant (plus you need a VM software, the instructions assume VirtualBox) and Ansible

  • Install Vagrant locally, add to Path
  • Follow the installation instructions on this useful blog entry, leave out the Eclipse specifics if you don’t need them. You’ll need to adapt the vagrantfile example to use another VM provider if you’re not using VirtualBox
  • Get a Ubuntu box for Vagrant, e.g. this one here
  • Use this box in the Vagrant file from the installation instructions, be sure to use a file:/// prefix for the local box url.
  • At this point, you can already “vagrant up” in the directory where the vagrant file is located, and “vagrant ssh” into the machine you’ve created (user vagrant, password the same). You can also ssh from cygwin with “ssh vagrant@127.0.0.1 -p 2222”
  • In order to configure your Ansible hosts, create a “ansibleHosts” file in the directory where you’re working with Ansible, and add the following
    [local]
    127.0.0.1
    
  • To configure the vagrant ssh port (which is 2222), create a file “ansible.cfg” with the contents
    [defaults]
    remote_port = 2222
    
  • In order for Ansible to ssh into the VM, you’ll want to set up ssh keys. In cygwin execute “keygen -t rsa”, which creates a public/private key pair
  • Deploy the ssh key to your VM by running “ssh-copy-id -i /home/YOURUSER/.ssh/id_rsa.pub vagrant@127.0.0.1 -p 2222”
  • Create a Ansible playbook, e.g. this one here, it connects to the specified hosts (local from the ansibleHosts file earlier) and installs the phantomjs package (via sudo):
    - hosts: local
      remote_user: vagrant
      sudo: true
      tasks:
        - apt: pkg=phantomjs state=present
    
  • run the playbook with “ansible-playbook -i ansibleHosts playbook.yml” (which runs the playbook file using the Ansible hosts specified in ansibleHosts file)

Update: after some usage, it seems there’s some non-deterministic problems with SSH which stops the process in-between sometimes – broken pipes and the likes – so running Ansible with cygwin under Windows is (as of now, with this setup) not really production-ready. Better spin up (another) a Linux VM and use Ansible from there.

This entry was posted in Software. Bookmark the permalink.