Rubendob
Rubendob

Reputation: 1704

Ansible & Vagrant: deploy in local and production with same playbook

I've been working in a recipe for vagrant+virtualbox+ansible lately. I have some issues now I want to reuse the code of the recipe to be able to provision not just the local but the staging and production environment. My playbook is:

- hosts: all
  sudo: true
  pre_tasks:
  roles:
    - common
    - webserver
    - database
    - php
  post_tasks:

and my Vagrantfile

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

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/precise64"
  config.vm.network "private_network", ip: "10.10.10.101"
  config.vm.synced_folder ".", "/vagrant", type: "nfs"

  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.customize ["modifyvm", :id, "--memory", "512"]
    vb.name = "testing"
  end

  config.vm.provision :ansible do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.limit    = "local"
        ansible.inventory_path = "./inventory"
  end
end

So what happens? If I run

$ vagrant up

everything works fine but if I run

$ ansible-playbook -i inventory playbook.yml --limit production

(where production is a group in inventory with a remote ip on ovh)

it fails because the playbook will need

user:root

parameter, but if I put this parameter on the playbook, then it fails when I run

$ vagrant provision

I know making some yml for local and remote will finish the issue, but I would prefer more elegant solution.

Any ideas?

thanks

Upvotes: 1

Views: 1451

Answers (2)

hkariti
hkariti

Reputation: 1719

You can customize the ssh user per host in the inventory using group vars:

in group_vars/local:

ansible_ssh_user: vagrant

in group_vars/production:

ansible_ssh_user: root

Also consider using separate inventory files for vagrant and production. It doesn't allow you to run things on both envs at once, but that's hardly a common use case, and it's safer.

Upvotes: 2

BMW
BMW

Reputation: 45293

Take a look on the offical document: Using Vagrant and Ansible

try with -u root

$ ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --private-key=~/.vagrant.d/insecure_private_key -u vagrant playbook.yml

Upvotes: 0

Related Questions