如何在远程系统上使用Ansible任务移动/重命名文件 [英] How to move/rename a file using an Ansible task on a remote system
问题描述
如何使用远程系统上的Ansible模块移动/重命名文件/目录?我不想使用命令/shell任务,也不想将文件从本地系统复制到远程系统.
How is it possible to move/rename a file/directory using an Ansible module on a remote system? I don't want to use the command/shell tasks and I don't want to copy the file from the local system to the remote system.
推荐答案
文件模块不会在远程系统上复制文件. src参数仅在创建到文件的符号链接时由文件模块使用.
The file module doesn't copy files on the remote system. The src parameter is only used by the file module when creating a symlink to a file.
如果您想完全在远程系统上移动/重命名文件,那么最好的选择是使用命令模块来调用相应的命令:
If you want to move/rename a file entirely on a remote system then your best bet is to use the command module to just invoke the appropriate command:
- name: Move foo to bar
command: mv /path/to/foo /path/to/bar
如果想花哨的话,可以先使用stat模块检查foo是否确实存在:
If you want to get fancy then you could first use the stat module to check that foo actually exists:
- name: stat foo
stat: path=/path/to/foo
register: foo_stat
- name: Move foo to bar
command: mv /path/to/foo /path/to/bar
when: foo_stat.stat.exists
这篇关于如何在远程系统上使用Ansible任务移动/重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!