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

查看:146
本文介绍了如何在远程系统上使用 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天全站免登陆