Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

まとめ

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/にコピーされる

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした