如何在远程系统上使用Ansible任务移动/重命名文件 [英] How to move/rename a file using an Ansible task on a remote system

查看:1499
本文介绍了如何在远程系统上使用Ansible任务移动/重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用远程系统上的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆