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?
You can use vault_password_file
option.
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
.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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With