Ansible 无法在远程服务器上复制文件,但如果从命令行 运行 则命令 运行s 正确

Ansible can't copy files on remote server, but the command runs correctly if run from command line

我正在尝试将 /opt/* 下的所有内容移动到远程服务器上的新位置。我已经尝试过直接使用 运行 rsync 的命令,以及同时使用复制和同步 ansible 模块。在所有情况下,我都会收到相同的错误消息:

 "msg": "rsync: link_stat \"/opt/*\" failed: No such file or directory

如果我 运行 直接在我的远程服务器上运行 ansible 错误消息的“cmd”部分中列出的命令,它可以正常工作。我不确定为什么 ansible 会失败。

这是当前使用同步的尝试:

- name: move /opt to new partition
  become: true
  synchronize:
    src: /opt/*
    dest: /mnt/opt/*
  delegate_to: "{{ inventory_hostname }}"

您应该跳过常见错误的通配符:

更新 感谢用户:@Zeitounator,我设法通过同步来做到这一点。 使用 synchronize 而不是 copy 模块的优点是性能,如果你有很多文件要复制,它会快得多。

- name: move /opt to new partition
      become: true
      synchronize:
        src: /opt/
        dest: /mnt/opt
      delegate_to: "{{ inventory_hostname }}"

所以基本上最初的答案是正确的,但你需要删除通配符“*”和目标路径上的斜杠。 此外,您应该在 /opt/

上添加文件删除