ansible(6)--ansible的copy和fetch模块

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
    
posted @ 2021-08-18 16:49  向往自由的独行者  阅读(1482)  评论(0编辑  收藏  举报