资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ansile(2)模块之synchronize

ansible synchronize作用跟rsync很相似,具体参数如下:

10年积累的网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有洮南免费网站建设让你可以放心的选择与我们合作。

参数内容,均来自官网,如果有出入,以官网为主

参数
是否必须默认选项说明
archivenoyes

yes

no

rsync镜像递归标志,启用递归,链接,权限,时间,属主,属组标志和 -D
checksumnono

yes

no

跳过基本的校验
compressnoyes

yes

no

在传输过程中压缩文件数据。在大多数情况下,请保持启用,除非它导致问题。
copy_linksnono

yes

no

将符号链接复制为它们所指向的项(引用对象),而不是符号链接。
deletenono

yes

no

删除src路径中不存在的dest中的文件(传输后,不在之前)。此选项需要recursive = yes。
destyes

要同步源的目标主机上的路径;该路径可以是绝对路径或相对路径。
dest_portno ansible_ssh_port的值设定,remote_port配置设置,如果ssh客户端没有设置的话
目标主机上ssh的端口号。在ansible 2.0之前,ansible_ssh_port库存变量优先于此值。
dirsnono

yes

no


传输目录而不进行递归
existing_onlynono

yes

no

跳过在接收器上创建一个新文件。
groupno递归选项的值

yes

no

保留组
linksno递归选项的值

yes

no

将符号链接复制为符号链接。
modenopush

yes

no

指定同步的方向。在推模式下,localhost或delegate是源;在拉模式下,远程主机在上下文中是源。
ownerno递归选项的值

yes

no

保留属主
partialno

告诉rsync保留一些文件,这应该使以后的文件传输的其余部分。
permsno递归选项的值

yes

no

保留权限
recursiveno递归选项的值

yes

no

递归到目录。
rsync_optsno

通过传入数组来指定其他rsync选项。
rsync_pathno

指定要在远程主机上运行的rsync命令。请参见rsync手册页上的--rsync-path。
rsync_timeoutno

以秒为单位指定rsync命令的--timeout。
set_remote_usernoTrue
put user @为远程路径。如果您有自定义ssh配置为与库存用户不匹配的主机定义远程用户,则应将此参数设置为“no”。
srcyes

将同步到目标的源主机上的路径;路径可以是绝对路径或相对路径。
timesno递归选项的值

yes

no

保存修改时间
use_ssh_argsnono

yes

no

使用ansible.cfg中指定的ssh_args
verify_hostno

验证目标主机密钥。

 

1.同步目录下文件:

源文件:

# ll
total 4
lrwxrwxrwx 1 root root    8 Dec 28 17:39 fk -> /data/fm
-rw-r--r-- 1 root root 4096 May 31  2016 p.txt

同步目录下文件,而不是目录:

# ansible webserver -m synchronize -a "src=/root/dog/ dest=/tmp/"
172.16.110.48 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.48:/tmp/\"", 
    "msg": ".d..t...... ./\n", 
    "rc": 0, 
    "stdout_lines": [
        ".d..t...... ./"
    ]
}
172.16.110.47 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.47:/tmp/\"", 
    "msg": ".d..t...... ./\n", 
    "rc": 0, 
    "stdout_lines": [
        ".d..t...... ./"
    ]
}

同步了所有文件

# ll
total 8
drwxr-xr-x  2 root root   29 Dec 28 17:39 dog
lrwxrwxrwx  1 root root    8 Dec 28 17:39 fk -> /data/fm //无效链接,源不存在

2.同步整个目录:

# ansible webserver -m synchronize -a "src=/root/dog dest=/tmp/" 
172.16.110.48 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog\" \"172.16.110.48:/tmp/\"", 
    "msg": "cd+++++++++ dog/\ncL+++++++++ dog/fk -> /data/fm\n /data/fm", 
        " {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog\" \"172.16.110.47:/tmp/\"", 
    "msg": "cd+++++++++ dog/\ncL+++++++++ dog/fk -> /data/fm\n /data/fm", 
        "

同步了整个目录

# ll
total 8
drwxr-xr-x  2 root root   29 Dec 28 17:39 dog

3.同步时候保持属主,属组,权限,链接,同时传输的时候使用压缩

同步前文件详情:

# ll dog/
total 8
-rw-r--r-- 1 ckl  ckl     9 Dec 29 13:05 em_f
lrwxrwxrwx 1 root root    8 Dec 28 17:39 fk -> /data/fm
-rwx------ 1 root root 4096 May 31  2016 p.txt

同步:

# ansible webserver -m synchronize -a "compress=yes group=yes links=yes  src=/root/dog/ dest=/tmp/sync_dir"         
172.16.110.47 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.47:/tmp/sync_dir\"", 
    "msg": ".d..t...... ./\n /data/fm\n /data/fm", 
        " {
"changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.48:/tmp/sync_dir\"", 
    "msg": ".d..t...... ./\n /data/fm\n /data/fm", 
        "

    

结果:(权限及属性保持不变)

# ll
total 8
-rw-r--r-- 1 ckl  ckl     9 Dec 29 13:05 em_f
lrwxrwxrwx 1 root root    8 Dec 28 17:39 fk -> /data/fm //无效链接,源不存在
-rwx------ 1 root root 4096 May 31  2016 p.txt

4.删除目标上的文件:

删除前文件:

# ll
total 8
-rw-r--r-- 1 ckl  ckl     9 Dec 29 13:05 em_f
lrwxrwxrwx 1 root root    8 Dec 28 17:39 fk -> /data/fm
-rwx------ 1 root root 4096 May 31  2016 p.txt

删除:

# ansible webserver -m synchronize -a "src=/root/dele/ dest=/tmp/sync_dir delete=yes"
172.16.110.47 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dele/\" \"172.16.110.47:/tmp/sync_dir\"", 
    "msg": "*deleting   dele/\n*deleting   p.txt\n*deleting   fk\n*deleting   em_f\n", 
    "rc": 0, 
    "stdout_lines": [
        "*deleting   dele/", 
        "*deleting   p.txt", 
        "*deleting   fk", 
        "*deleting   em_f"
    ]
}
172.16.110.48 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dele/\" \"172.16.110.48:/tmp/sync_dir\"", 
    "msg": "*deleting   dele/\n*deleting   p.txt\n*deleting   fk\n*deleting   em_f\n", 
    "rc": 0, 
    "stdout_lines": [
        "*deleting   dele/", 
        "*deleting   p.txt", 
         "*deleting   fk", 
        "*deleting   em_f"
    ]
}

查看目标文件:

# ll /tmp/sync_dir/
total 0

5.同步链接时,同步源文件

# ll
total 8
-rw-r--r-- 1 ckl  ckl     9 Dec 29 13:05 em_f
lrwxrwxrwx 1 root root    8 Dec 28 17:39 fk -> /data/fm
-rwx------ 1 root root 4096 May 31  2016 p.txt
drwxr-xr-x 2 root root   20 Dec 29 13:15 wu
[root@localhost dog]# ll *
-rw-r--r-- 1 ckl  ckl     9 Dec 29 13:05 em_f
lrwxrwxrwx 1 root root    8 Dec 28 17:39 fk -> /data/fm
-rwx------ 1 root root 4096 May 31  2016 p.txt
wu:
total 4
-rw-r--r-- 1 root root 14 Dec 29 13:15 kk.txt

同步:

# ansible webserver -m synchronize -a "compress=yes group=yes copy_links=yes dirs=yes src=/root/dog/ dest=/tmp/sync_dir" 
172.16.110.48 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --copy-links --dirs --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.48:/tmp/sync_dir\"", 
    "msg": ".d..t...... ./\n {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --copy-links --dirs --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.47:/tmp/sync_dir\"", 
    "msg": ".d..t...... ./\n

查看结果:

# ll
total 12
-rw-r--r-- 1 ckl  ckl     9 Dec 29 13:05 em_f
-rw-r--r-- 1 root root    7 Dec 28 16:49 fk //不再是链接
-rwx------ 1 root root 4096 May 31  2016 p.txt
drwxr-xr-x 2 root root   20 Dec 29 13:15 wu

当前题目:ansile(2)模块之synchronize
链接地址:http://cdkjz.cn/article/gchohh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220