LoginSignup
3
7

More than 5 years have passed since last update.

備忘録 Ansibleファイルをコピーする(copyモジュール)

Last updated at Posted at 2018-01-15

Chefの場合、fileリソースなどを使って、サーバーから対象ノードにファイルをコピーしていましたが、Ansibleの場合、どうやって行うのか?ということで、テストしてみました。

Ansibleの場合は、copyモジュールを使うことで対応できます。

1. テスト用ファイルを作成

コピーするテスト用ファイルを作成します。
ファイル操作で利用するファイルは、filesというディレクトリを作成し、その中に格納することにしました。
 ※chefのcookbook名/filesディレクトリのイメージ

/root/
   └ ansible
       ├─ hosts             ・・・inventoryファイル
       ├─ tasks             ・・・playbook格納場所
       │   └─ test2.yml     ・・・playbook
       └─ files             ・・・ファイル系操作用ディレクトリ
           └─ test2.txt     ・・・テスト用ファイル

作成したtest2.txtの内容は以下の通りです。
(とりあえず確認用のため、超簡単

test comment1

2.playbookの作成

ファイルをコピーするplaybookを作成します。

test2.yml
---
- hosts: web                            ・・・今回はwebサーバーに対して実施
  sudo: yes
  tasks:
   - name: copy a directory
     copy:                ・・・copyモジュールを利用
       src: ../files/test2.txt      ・・・テストファイルのパスを記述
       dest: /tmp            ・・・対象ノードへコピーするパスを記述
       mode: 0577            ・・・とりあえずパーミッションでも適当に付けてみる

3.playbookの実行

1. playbookを実行

作成したplaybookを実行します。

ansible-playbook -i hosts ./task/test2.yml

※実行結果

# ansible-playbook -i hosts ./task/test2.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is
'sudo' (default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.

PLAY [web] ************************************************************************************************

TASK [Gathering Facts] ************************************************************************************
ok: [brighton002]

TASK [copy a directory] ***********************************************************************************
changed: [brighton002]

PLAY RECAP ************************************************************************************************
brighton002                : ok=2    changed=1    unreachable=0    failed=0

正しく実行されたことを確認しました。

2.対象ノードでの確認

対象ノードにログインし、ファイルのパーミッションと中身を確認します。

  • ファイルのパーミッション

playbookで記述したとおり、パーミッションが0577であることを確認する。

# ls -l
total 4
-r-xrwxrwx 1 root root 14 Jan 15 16:04 test2.txt
  • ファイルの中身の確認

ファイルの中身を確認する。

# cat test2.txt
test comment1

3.冪統性の確認

再度playbookを実行してみる。
冪統性により、copyモジュールは実行されない。

ansible-playbook -i hosts ./task/test2.yml

※実行結果

# ansible-playbook -i hosts ./task/test2.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is
'sudo' (default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.

PLAY [web] ************************************************************************************************

TASK [Gathering Facts] ************************************************************************************
ok: [brighton002]

TASK [copy a directory] ***********************************************************************************
ok: [brighton002]

PLAY RECAP ************************************************************************************************
brighton002                : ok=2    changed=0    unreachable=0    failed=0

changed=0であることから、変更箇所が無い事が分かる。

4.テストファイルを編集し、再度実行

test2.txtの中身を変更する。

test comment1
test comment2  ← 追加したコメント

playbookを実行してみる。

ansible-playbook -i hosts ./task/test2.yml

※実行結果

# ansible-playbook -i hosts ./task/test2.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is
'sudo' (default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.

PLAY [web] ************************************************************************************************

TASK [Gathering Facts] ************************************************************************************
ok: [brighton002]

TASK [copy a directory] ***********************************************************************************
changed: [brighton002]

PLAY RECAP ************************************************************************************************
brighton002                : ok=2    changed=1    unreachable=0    failed=0

changed=1となり変更されたことが分かる。

対象ノードで確認してみる。

  • ファイルのタイムスタンプの確認
# ls -l
total 4
-r-xrwxrwx 1 root root 28 Jan 15 17:02 test2.txt

タイムスタンプが変わっていることがわかる(実行前は16:04)

  • ファイルの中身の確認
# cat test2.txt
test comment1
test comment2

コメントが1行追加されたことがわかる。

3
7
1

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
3
7