トップページへ

ansibleのcopyモジュールでディレクトリを生成する方法

小粋空間 » Linux » ansible » ansibleのcopyモジュールでディレクトリを生成する方法

ansibleのcopyモジュールでディレクトリを生成する方法を紹介します。

1.問題点

ansibleのcopyモジュールでファイルをコピーしたいのですが、コピー先のディレクトリがなく、エラーになります。

試したのは下記の2種類です。

test.yml

---
- name: テスト
  copy:
    src: /tmp/hoge.txt
    dest: /tmp/aaa/bbb/hoge.txt2
    remote_src: yes

または

---
- name: テスト
  copy:
    src: /tmp/hoge.txt
    dest: /tmp/aaa/bbb
    remote_src: yes

これを実行すると、下記のように"Destination directory /tmp/aaa/bbb does not exist"というエラーになります。

$ ansible-playbook -i inventory test.yml
 
PLAY [act] ***************************************************************************************************************************************
 
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [server]
 
TASK [test : test.ymlをインクルード] *************************************************************************************************************
included: /var/tmp/ansible/roles/test/tasks/test.yml for server
 
TASK [test : テスト] *****************************************************************************************************************************
fatal: [server]: FAILED! => {"changed": false, "msg": "Destination directory /tmp/aaa/bbb does not exist"}
 
TASK [test : エラーが発生したため停止] ***********************************************************************************************************
fatal: [server]: FAILED! => {"changed": false, "msg": "ロール実行中にエラー発生"}
 
PLAY RECAP ***************************************************************************************************************************************
server               : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=1    ignored=0

ということで、ansibleのcopyモジュールでディレクトリを生成する方法を紹介します。

2.ansibleのcopyモジュールでディレクトリを生成する

ansibleのcopyモジュールでディレクトリを生成するには、destオプションを"/"で終わらせます。

---
- name: テスト
  copy:
    src: /tmp/hoge.txt
    dest: /tmp/aaa/bbb/
    remote_src: yes

このansibleを実行すると正常にディレクトリを生成してくれます。

$ ansible-playbook -i inventory test.yml
 
PLAY [act] ***************************************************************************************************************************************
 
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [server]
 
TASK [test : test.ymlをインクルード] *************************************************************************************************************
included: /var/tmp/ansible/roles/test/tasks/test.yml for server
 
TASK [test : テスト] *****************************************************************************************************************************
changed: [server]
 
PLAY RECAP ***************************************************************************************************************************************
server               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

« 前の記事へ

次の記事へ »

トップページへ