1. copy模塊
功能:從 ansible
服務端主控端復制文件到遠程主機;
copy
模塊的主要參數如下:
參數 | 說明 |
---|---|
src | 復制的源文件路徑,若源文件為目錄,默認進行遞歸復制,如果路勁以“/”結尾,僅會復制目錄下的內容,該目錄本身不會復制,如果路徑不帶“/”,目錄本身和目錄下的內容會一並復制過去。 |
dest | 目標絕對路徑,如果源是文件夾,目標也必須是文件夾,不存在將創建 |
backup | 如果目標主機已經有源文件,會事先備份,防止覆蓋 |
mode | 文件復制到遠程並設定權限,默認file=644,directory=755 |
owner | 文件復制到遠程並設定屬主,默認為root |
group | 文件復制到遠程並設定屬組,默認為root |
content | 將目標文件的內容,指定為content所帶的字符串 |
-
示例一:把
/data/nginx/html/web01/index.html
復制到被控主機/tmp
目錄下,屬主屬組為nginx
,權限為644
:[root@xuzhichao ~]# ansible NginxWebs -m copy -a "src=/data/nginx/html/web01/index.html dest=/tmp owner=nginx group=nginx mode=644" 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "b346efabe3aa64027fd74cceabfe9548989cea00", "dest": "/tmp/index.html", "gid": 887, "group": "nginx", "md5sum": "022cb99565535954e448519329778662", "mode": "0644", "owner": "nginx", "size": 7, "src": "/root/.ansible/tmp/ansible-tmp-1627787786.2-6672-67801950378810/source", "state": "file", "uid": 887 } 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "b346efabe3aa64027fd74cceabfe9548989cea00", "dest": "/tmp/index.html", "gid": 887, "group": "nginx", "md5sum": "022cb99565535954e448519329778662", "mode": "0644", "owner": "nginx", "size": 7, "src": "/root/.ansible/tmp/ansible-tmp-1627787786.2-6673-128530778543575/source", "state": "file", "uid": 887 } [root@nginx03 ~]# ll /tmp/index.html -rw-r--r-- 1 nginx nginx 7 Aug 1 11:16 /tmp/index.html
-
示例二:再次復制上例文件,並對原文件備份:
[root@xuzhichao ~]# ansible NginxWebs -m copy -a "src=/data/nginx/html/web01/index.html dest=/tmp owner=nginx group=nginx mode=644 backup=yes" 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "backup_file": "/tmp/index.html.6043.2021-08-01@11:20:39~", "changed": true, "checksum": "f8834b5232582035f785a3dc77a6303e339d1157", "dest": "/tmp/index.html", "gid": 887, "group": "nginx", "md5sum": "747df44881df1860170c66321f38ce4c", "mode": "0644", "owner": "nginx", "size": 10, "src": "/root/.ansible/tmp/ansible-tmp-1627788037.97-6797-37444206141079/source", "state": "file", "uid": 887 } 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "backup_file": "/tmp/index.html.7789.2021-08-01@11:20:39~", "changed": true, "checksum": "f8834b5232582035f785a3dc77a6303e339d1157", "dest": "/tmp/index.html", "gid": 887, "group": "nginx", "md5sum": "747df44881df1860170c66321f38ce4c", "mode": "0644", "owner": "nginx", "size": 10, "src": "/root/.ansible/tmp/ansible-tmp-1627788037.98-6796-83776884129851/source", "state": "file", "uid": 887 } [root@nginx03 ~]# ll /tmp/index.html* -rw-r--r-- 1 nginx nginx 10 Aug 1 11:20 /tmp/index.html -rw-r--r-- 1 nginx nginx 7 Aug 1 11:16 /tmp/index.html.6043.2021-08-01@11:20:39~ <==備份的原文件
-
往遠程的主機文件中寫入內容,如果文件不存在則創建:
[root@xuzhichao ~]# ansible NginxWebs -m copy -a "content="Http_Server\n" dest=/var/www/html/index.html" 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "50d275e32df9f317f802475b11627144db2f5d3e", "dest": "/var/www/html/index.html", "gid": 0, "group": "root", "md5sum": "4ab65dcd3bf954b6e54e9ff439721ae8", "mode": "0644", "owner": "root", "size": 11, "src": "/root/.ansible/tmp/ansible-tmp-1627788165.69-6855-262538185402060/source", "state": "file", "uid": 0 } 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "50d275e32df9f317f802475b11627144db2f5d3e", "dest": "/var/www/html/index.html", "gid": 0, "group": "root", "md5sum": "4ab65dcd3bf954b6e54e9ff439721ae8", "mode": "0644", "owner": "root", "size": 11, "src": "/root/.ansible/tmp/ansible-tmp-1627788165.71-6854-248696166101546/source", "state": "file", "uid": 0 } [root@nginx03 ~]# cat /var/www/html/index.html Http_Server
-
示例四:復制目錄到目標主機:
[root@xuzhichao ~]# ansible NginxWebs -m copy -a "src=/root/test dest=/root" 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "feba0582dfea7fc4fd236536b6a1173b19228388", "dest": "/root/test/nginx.conf", "gid": 0, "group": "root", "md5sum": "482efd688106eebebda54c12e52ddd01", "mode": "0644", "owner": "root", "size": 3258, "src": "/root/.ansible/tmp/ansible-tmp-1627788373.43-6930-280408452171512/source", "state": "file", "uid": 0 } 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "feba0582dfea7fc4fd236536b6a1173b19228388", "dest": "/root/test/nginx.conf", "gid": 0, "group": "root", "md5sum": "482efd688106eebebda54c12e52ddd01", "mode": "0644", "owner": "root", "size": 3258, "src": "/root/.ansible/tmp/ansible-tmp-1627788373.42-6929-226478301245663/source", "state": "file", "uid": 0 } [root@nginx03 ~]# ll /root/test/ total 4 -rw-r--r-- 1 root root 3258 Aug 1 11:26 nginx.conf
2. fetch模塊
作用:從客戶端取文件(只能是文件,不支持目錄)至服務器端的目錄里,與copy
相反,如果一定要拉取目錄,可以先將目錄tar,再拉取。
主要參數如下:
參數 | 說明 |
---|---|
src | 復制的源文件路徑,源文件只能是文件 |
dest | 目標絕對路徑 |
-
示例一:把被控主機的
/etc/nginx/nginx/conf
配置文件拷貝到本機的/root/nginx
目錄下:[root@xuzhichao ~]# ansible NginxWebs -m fetch -a 'src=/etc/nginx/nginx.conf dest=/root/nginx' 192.168.20.22 | CHANGED => { "changed": true, "checksum": "feba0582dfea7fc4fd236536b6a1173b19228388", "dest": "/root/nginx/192.168.20.22/etc/nginx/nginx.conf", "md5sum": "482efd688106eebebda54c12e52ddd01", "remote_checksum": "feba0582dfea7fc4fd236536b6a1173b19228388", "remote_md5sum": null } 192.168.20.23 | CHANGED => { "changed": true, "checksum": "b9671ff7a350dbf83543ec585e776500d45dccf0", "dest": "/root/nginx/192.168.20.23/etc/nginx/nginx.conf", "md5sum": "ee4c9a5f234057114b6454055df5f3a5", "remote_checksum": "b9671ff7a350dbf83543ec585e776500d45dccf0", "remote_md5sum": null } #注意:把遠程主機文件拷貝到本機時,會為每個遠程主機建立一個文件夾,名稱就是該遠程主機的ip地址,然后把文件分別放到對應主機的目錄下; [root@xuzhichao ~]# tree /root/nginx /root/nginx ├── 192.168.20.22 │ └── etc │ └── nginx │ └── nginx.conf └── 192.168.20.23 └── etc └── nginx └── nginx.conf 6 directories, 2 files
-
示例二:拷貝遠程主機的目錄到本機
#1.首先需要把遠程主機的目錄打包,默認打包到遠程主機的家目錄下(/root) [root@xuzhichao ~]# ansible NginxWebs -m shell -a 'tar jcf log.tar.bzip2 /var/log/nginx/access*' [WARNING]: Consider using the unarchive module rather than running 'tar'. If you need to use command because unarchive is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. 192.168.20.23 | CHANGED | rc=0 >> tar: Removing leading `/' from member names 192.168.20.22 | CHANGED | rc=0 >> tar: Removing leading `/' from member names #2.把打包文件取回本機: [root@xuzhichao ~]# ansible NginxWebs -m fetch -a 'src=/root/log.tar.bzip2 dest=/root/nginx' 192.168.20.23 | CHANGED => { "changed": true, "checksum": "832b6b9863baf60970f0972c749df8a26221ab26", "dest": "/root/nginx/192.168.20.23/root/log.tar.bzip2", "md5sum": "86c762c3ab6f3253c6b6be1739a95ac8", "remote_checksum": "832b6b9863baf60970f0972c749df8a26221ab26", "remote_md5sum": null } 192.168.20.22 | CHANGED => { "changed": true, "checksum": "c66537be74f74e1c03254be3400ce7af4a1fe4bb", "dest": "/root/nginx/192.168.20.22/root/log.tar.bzip2", "md5sum": "74671b6a38285ae2d576451b3b248f3e", "remote_checksum": "c66537be74f74e1c03254be3400ce7af4a1fe4bb", "remote_md5sum": null } [root@xuzhichao ~]# tree /root/nginx /root/nginx ├── 192.168.20.22 │ ├── etc │ │ └── nginx │ │ └── nginx.conf │ └── root │ └── log.tar.bzip2 └── 192.168.20.23 ├── etc │ └── nginx │ └── nginx.conf └── root └── log.tar.bzip2 8 directories, 4 files