LoginSignup
27
33

More than 5 years have passed since last update.

Ansibleでローカルのディレクトリをリモートにコピーする

Posted at

まとめ

Ansibleでローカルのファイル/ディレクトリをリモートに持っていくためには
synchronizeではなくcopyモジュールを使えば良い

synchronizecopy

ansible directory copy - Google 検索すると、copysynchronizeの2つのコマンドが出てくるkobito.1411812903.512220.png

copy

ファイルをコピーするためのコマンドっぽい
documentのサンプルでも

sample
# Example from Ansible Playbooks
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644

# The same example as above, but using a symbolic mode equivalent to 0644
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u=rw,g=r,o=r"

# Another symbolic mode example, adding some permissions and removing others
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u+rw,g-wx,o-rwx"

# Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
- copy: src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes

# Copy a new "sudoers" file into place, after passing validation with visudo
- copy: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'

全てファイルのコピーを行っている

synchronize

rsyncコマンドを実行するモジュールっぽい

sample
# Synchronization of src on the control machine to dest on the remote hosts
synchronize: src=some/relative/path dest=/some/absolute/path

# Synchronization without any --archive options enabled
synchronize: src=some/relative/path dest=/some/absolute/path archive=no

# Synchronization with --archive options enabled except for --recursive
synchronize: src=some/relative/path dest=/some/absolute/path recursive=no

recursiveとかあるし、ディレクトリを持って行くにはsynchronizeモジュールが適切っぽく思われる

しかし、synchronizeで実行すると、

Error
msg: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.1]

と出てしまってコピー出来なかった。
sshを使ったrsyncのはずが、何故かパスワードを求められていて、Permission deniedとなってしまっている。

ディレクトリのコピー

最初のまとめにも書いた通り、

copy: src={{ src_dir }} dest={{ dest_dir }}

だけで良かった。

なお、ディレクトリを丸ごとコピーするには

vars/main.yml
src_dir: /src/path/local_dir
dest_dir: /dest/path/remot_dir

とする。
これでローカルの/src/path/local_dirというディレクトリが、リモートの/dest/path/remote_dirという名前でコピーされる

vars/main.yml
src_dir: /src/path/local_dir/
dest_dir: /dest/path/remot_dir/

とすると、
/src/path/local_dir/*/dest/path/remote_dir/にコピーされる

27
33
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
27
33