Micah
Micah

Reputation: 10395

How to pass ansible variables into vagrant

Let's say i have a playbook that i normally run as follows:

ansible-playbook -i hosts -e "var1=val1" \
    -e "hosts=all" \
    -e "devops_dir=$DEVOPS_PATH" \
    -e "cname=something.gooten.com" \
    -v playbook.yml

And i want to have this run when i startup a Vagrantfile--

  #
  # Run Ansible from the Vagrant Host
  #
  config.ssh.insert_key = false
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.verbose = "v"
  end

How do i add the -e variables into the Vagrantfile so that they are called as well?

Upvotes: 3

Views: 5096

Answers (2)

autra
autra

Reputation: 962

If you want to be able to pass arbitrary args to ansible, say -e args as well as -t, -v etc, you can use this trick:

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    # use args from `ANSIBLE_ARGS` env variable
    ansible.raw_arguments = Shellwords.shellsplit(ENV['ANSIBLE_ARGS']) if ENV['ANSIBLE_ARGS']
  end

And then, invoke vagrant provision as such:

ANSIBLE_ARGS="-v -e var1=val1 -t mytag" vagrant provision

Upvotes: 0

Baptiste Mille-Mathias
Baptiste Mille-Mathias

Reputation: 2169

You have to use extra_vars

ansible.extra_vars = {
  var1: val1,
  hosts: all,
  devops_dir: $DEVOPS_PATH,
  cname: something.gooten.com
}

Upvotes: 4

Related Questions