檔案傳輸 - copy 模組

  1. Ansible 可以用並行的方式同時 scp 大量的檔案到多台主機,完整文件可用 ansible-doc 查詢 copy 模組文件。
    [deyu1@kvm19  ansible]$ ansible-doc copy
    
  2. 先編輯一個測試檔 adhoc.txt
    [deyu1@kvm19 ansible]$ vim adhoc.txt
    [deyu1@kvm19 ansible]$ cat adhoc.txt
    ad-hoc file transfer
    
  3. 使用 copy 模組將 adhoc.txt 複製到 prod 群組裡所有主機的 /home/deyu1 目錄:
    [deyu1@kvm19 ansible]$ ansible prod -m copy -a "src=adhoc.txt dest=/home/deyu1/"
    
    kvm14 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "5ced2367ff60e242577e9a5d90a61233d9336c97",
        "dest": "/home/deyu1/adhoc.txt",
        "gid": 0,
        "group": "root",
        "md5sum": "bb660c5cbe1c8defa638bd8e7592b48a",
        "mode": "0644",
        "owner": "root",
        "secontext": "unconfined_u:object_r:user_home_t:s0",
        "size": 21,
        "src": "/root/.ansible/tmp/ansible-tmp-1585003071.5122902-76118385004573/source",
        "state": "file",
        "uid": 0
    }
    kvm13 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "5ced2367ff60e242577e9a5d90a61233d9336c97",
        "dest": "/home/deyu1/adhoc.txt",
        "gid": 0,
        "group": "root",
        "md5sum": "bb660c5cbe1c8defa638bd8e7592b48a",
        "mode": "0644",
        "owner": "root",
        "secontext": "unconfined_u:object_r:user_home_t:s0",
        "size": 21,
        "src": "/root/.ansible/tmp/ansible-tmp-1585003071.3513613-64722127143810/source",
        "state": "file",
        "uid": 0
    }
    
  4. 使用 shell 模組查看 prod 群組裡所有主機,有複製過來的檔案 adhoc.txt,由於 ansible.cfg 設定遠端用戶為 root,所以擁有者及群組皆為 root。
    [deyu1@kvm19 ansible]$ ansible prod -m shell -a "ls -l /home/deyu1/adhoc.txt"
    kvm14 | CHANGED | rc=0 >>
    -rw-r--r--. 1 root root 21 Mar 24 06:37 /home/deyu1/adhoc.txt
    
    kvm13 | CHANGED | rc=0 >>
    -rw-r--r--. 1 root root 21 Mar 24 06:37 /home/deyu1/adhoc.txt