Pass vault password to vagrants ansible_local provisioner
Asked Answered
S

3

7

I'm using the ansible_local provisioner for my vagrant box. Some of my variables should be stored in a vault file.

While the ansible provisioner provides ask_vault_pass as configuration option (https://www.vagrantup.com/docs/provisioning/ansible.html#ask_vault_pass), the ansible_local does not.

Is there any workaround?

Steadfast answered 30/1, 2017 at 9:48 Comment(0)
W
9

You can use vault_password_file option.

1. echo to password file

Vagrant.configure(2) do |config|
  config.vm.box = '...'

  config.vm.provision :shell, inline: "echo 'password' > /tmp/vault_pass"

  config.vm.define :controller do |machine|
    ...

    machine.vm.provision 'ansible_local' do |ansible|
      ...
      ansible.vault_password_file = "/tmp/vault_pass"
      ...
    end
  end
end

2. use .synced_folder

Create vault_pass file, like following.

mkdir provision
cd provision
echo password > vault_pass

and Vagrantfile is following.

Vagrant.configure(2) do |config|
  config.vm.box = '...'

  config.vm.synced_folder "./provision", "/provision", id: "ansible", owner: "vagrant", group: "vagrant", mount_options: ["dmode=775,fmode=664"]

  config.vm.define :controller do |machine|
    ...

    machine.vm.provision 'ansible_local' do |ansible|
      ...
      ansible.vault_password_file = "/provision/vault_pass"
      ...
    end
  end
end
Wormy answered 16/2, 2017 at 11:14 Comment(0)
G
1

I suggest another approach to sujoyu's answer by asking the user to input the vault password when provisioning. Also inspired by this answer.

Vagrant.configure(2) do |config|

  config.vm.box = "..."

  # Password Input Function
  class Password
    def to_s
      begin
      system 'stty -echo'
      print "Ansible Vault Password: "
      pass = URI.escape(STDIN.gets.chomp)
      ensure
      system 'stty echo'
      end
      print "\n"
      pass
    end
  end

  # Ask for vault password
  config.vm.provision "shell", env: {"VAULT_PASS" => Password.new}, inline: <<-SHELL
    echo "$VAULT_PASS" > /tmp/vault_pass
  SHELL

  # Run ansible provision
  config.vm.provision "ansible_local" do |ansible|

      ansible.playbook = "playbook.yml"
      ansible.vault_password_file = "/tmp/vault_pass"

  end

  # Delete temp vault password file
  config.vm.provision "shell", inline: <<-SHELL
    rm /tmp/vault_pass
  SHELL

end
Gasper answered 7/7, 2018 at 18:58 Comment(0)
B
0

for vagrant version 2.2.9 using ansible.vault_password_file was resulting in

vault_password_file` does not exist on the host: 

use ask_vault_pass option

Vagrant.configure(2) do |config|
  config.vm.box = '...'

  config.vm.define :controller do |machine|
    ...

    machine.vm.provision 'ansible_local' do |ansible|
      ...
      ansible.ask_vault_pass = true
      ...
    end
  end
end
Basilicata answered 23/7, 2020 at 19:35 Comment(1)
This is not working with vagrant 2.2.19 :ansible local provisioner: The following settings shouldn't exist: ask_vault_passAyakoayala

© 2022 - 2024 — McMap. All rights reserved.