LoginSignup
25
20

More than 5 years have passed since last update.

Vagrant: synced_folderのrsyncで特定のファイルを除外したい

Last updated at Posted at 2015-05-25

Vagrantにはホストマシンのファイルをゲストマシンに同期する機能があります。これを使って、ホストのソースコードをゲストに同期しつつも、ゲスト側にいらないファイルは同期しないということをやってみます。

まず、ドキュメントに出てくる1つ目の例ですが、これはVagrantfileがあるディレクトリと同じディレクトリにある.gitを同期しないという指定になります。

Vagrant.configure("2") do |config|
  config.vm.synced_folder ".", "/vagrant", type: "rsync",
    rsync__exclude: ".git/"
end

もし、subproject/.gitを同期したくないときは、こうします。

Vagrant.configure("2") do |config|
  config.vm.synced_folder "subproject", "/subproject", type: "rsync",
    rsync__exclude: ".git/"
end

そして、複数のファイルを同期したくないときは配列にするとできます。

  config.vm.synced_folder "scala-project", "/scala-project", type: "rsync",
    rsync__exclude: [
      ".idea/",
      ".idea_modules/",
      "project/project/",
      "project/target/",
      "target/",
    ]

なお、Vagrantでは除外パスの先頭にスラッシュをつけると完全一致に、つけないと後方一致になるようです。

たとえば、下の例では、scala-project/*target/が除外されるようです。

  config.vm.synced_folder "scala-project", "/scala-project", type: "rsync",
    rsync__exclude: [
      "target/",
    ]

一方、下の例では完全一致になり、scala-project/target/が除外されます。

  config.vm.synced_folder "scala-project", "/scala-project", type: "rsync",
    rsync__exclude: [
      "/target/",
    ]

このへんの実装はvagrant/helper.rb at 808a86a918b73cf07bcc2823d09580840bf5b4cc · mitchellh/vagrantにあります。

25
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
20