ansible的copy模块四种拷贝情况

ansible的copy模块测试:
本地主机为m01,远程主机为web01.从m01拷贝文件到web01,本地m01情况:
[root@m01 rsync]# tree /test_dir/
/test_dir/
└── test_dir.txt

1、src是目录,dest是文件,报错,不允许这种情况:
[root@web01 tmp]# ll
总用量 0
-rw-r–r-- 1 root root 0 2月 12 12:36 test_dir.txt
用ll查看目的主机上存在test_dir.txt文件,执行ansible:
[root@m01 rsync]# ansible group2 -m copy -a “src=/test_dir dest=/tmp/test_dir.txt”
172.16.1.7 | FAILED! => {
“msg”: “Failed to get information on remote file (/tmp/test_dir.txt/test_dir/test_dir.txt): 不是目录”
}

2、src是目录,dest是目录 如果目的目录不存在,则自动创建:
[root@web01 tmp]# ll
总用量 0
-rw-r–r-- 1 root root 0 2月 12 12:36 test_dir.txt
用ll查看目的主机上/tmp下不存在test目录,执行ansible:
[root@m01 rsync]# ansible group2 -m copy -a “src=/test_dir dest=/tmp/test/”
在目的地的/tmp下原本没有/test目录存在,执行该条命令后变成:
[root@web01 tmp]# tree
.
└── test
└── test_dir
└── test_dir.txt
执行ansible命令时,发现目标主机上没有test目录存在,则会自动创建该目录,并把/test_dir目录及其下的文件 test_dir.txt一起拷贝过去;

3、src是文件,dest是目录,如果目的目录不存在,则自动创建:
[root@web01 tmp]# ll
总用量 0
drwxr-xr-x 3 root root 22 2月 12 12:19 test
ll查看到目标主机上/tmp下没有test_dir目录,执行ansible:
[root@m01 rsync]# ansible group2 -m copy -a “src=/test_dir/test_dir.txt dest=/tmp/test_dir/”
执行该命令后,会在/tmp下自动创建test_dir目录并把test_dir.txt 拷贝到该目录下:
[root@web01 tmp]# tree
.
├── test_dir
└── test_dir.txt

4、src是文件,dest是文件,如果目的文件所在目录不存在,则传输失败:
[root@web01 tmp]# ll
总用量 0
ll查看到目标主机上/tmp下没有test_dir目录,执行ansible:
[root@m01 rsync]# ansible group2 -m copy -a “src=/test_dir/test_dir.txt dest=/tmp/test_dir/test_dir.txt”
172.16.1.7 | FAILED! => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“checksum”: “da39a3ee5e6b4b0d3255bfef95601890afd80709”,
“msg”: “Destination directory /tmp/test_dir does not exist”
出错,提示目的目录不存在。

发布了15 篇原创文章 · 获赞 10 · 访问量 1244

猜你喜欢

转载自blog.csdn.net/ygh3110001606/article/details/104277000