4

我正在ansible_local为我的流浪者盒子使用供应商。我的一些变量应该存储在保险库文件中。

虽然 ansible 供应商提供ask_vault_pass了配置选项(https://www.vagrantup.com/docs/provisioning/ansible.html#ask_vault_pass),ansible_local但没有。

有什么解决方法吗?

4

3 回答 3

7

您可以使用vault_password_file选项。

1.回显到密码文件

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.使用.synced_folder

创建vault_pass文件,如下所示。

mkdir provision
cd provision
echo password > vault_pass

并且Vagrantfile正在关注。

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
于 2017-02-16T11:14:01.380 回答
1

对于 vagrant 版本 2.2.9 使用ansible.vault_password_file导致

vault_password_file` does not exist on the host: 

使用ask_vault_pass选项

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
于 2020-07-23T19:35:17.830 回答
1

我建议通过要求用户在配置时输入保险库密码来解决 sujoyu 的答案。也受到这个答案的启发。

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
于 2018-07-07T18:58:21.157 回答