Skip to content

Instantly share code, notes, and snippets.

@seansawyer
Last active September 21, 2015 22:11
Show Gist options
  • Save seansawyer/88a16373a3d2c2a939b1 to your computer and use it in GitHub Desktop.
Save seansawyer/88a16373a3d2c2a939b1 to your computer and use it in GitHub Desktop.
Vagrant + Ansible crash course

Install Virtualbox, Vagrant and Ansible.

Create a new directory for your first Vagrant VM. In that directory, create a Vagrantfile using vagrant init, and enable the Ansible provisioner by adding the following.

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "playbook.yml"
end

Now you can create an Ansible playbook named playbook.yml file in the same directory as your Vagrantfile, and vagrant provision will run it. Here's an easy one to start with that will update NSS and add the EPEL and IUS repositories.

- hosts: all
  remote_user: vagrant
  sudo: true
  tasks:  
  - name: ensure latest nss
    # This is necessary because old versions cause yum to fail
    # http://unix.stackexchange.com/questions/148144/unable-to-pull-epel-repository-metadata
    yum: name=nss state=latest
  - name: enable the EPEL repository
    yum: name=http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm state=present
  - name: enable the IUS repository
    yum: name=http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/ius-release-1.0-13.ius.centos6.noarch.rpm state=present
  - name: enable Nginx repo
    copy: dest=/etc/yum.repos.d/nginx.repo src=files/nginx.repo

Add aliases (in .bashrc or .bash_profile) to provide Ansible commands with the a Vagrant-generated inventory.

alias vansible="ansible -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --private-key=~/.vagrant.d/insecure_private_key -u vagrant"
alias vansible-playbook="ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --private-key=~/.vagrant.d/insecure_private_key -u vagrant"

Assuming you are in a directory with a Vagrantfile, this will give Ansible everything it needs to find your Vagrant hosts.

Now you can see what facts are available for a Vagrant host.

vansible default -m setup

These are all variables that can be referenced from playbooks and templates.

@seansawyer
Copy link
Author

Handy link to a really thorough playbook:
https://gist.github.com/marktheunissen/2979474

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment