とある地味なブログ

プログラミングとお絵かきに関する雑記。

Windonws上でVagrantとansibleを使ってつまづいた箇所

経緯

Windows上で開発用のVMを設定するのに、ansibleを使いたい。使いたくない?

Vagrant上でansible_localを使えばいけんじゃね!?

つまづきどころ

以下では、Vagrantで立ち上げるサーバはcontrollertargetで、

controllerがansibleをインストールするVM

targetがansibleを流されるVMとして、話を進める。

ask_vault_passオプションがねえ

開発用の環境なのでお行儀は悪い。

Vagrantfileにこんな感じで書く。

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

/tmp/vault_passファイルにパスワードを流し込んで、vault_password_fileにそのファイルを設定している。

Authentication failure.と出る

fatal: [192.168.x.x]: UNREACHABLE! => {"changed": false, "msg": "Authentication failed.", "unreachable": true}

これはVagrantのエラーではなく、ansibleのエラーのほう。

hostsファイルに以下のように書く。

[target]
192.168.x.x ansible_ssh_pass=vagrant ansible_ssh_user=vagrant

それと、controllerからtargetへ、一回SSHで繋いでおく。

$ vagrant ssh controller
[vagrant@controller ~] $ ssh <targetのIPアドレス>
yes
password: vagrant 

でおk。