带有 ansible 的 sshfs 给出的结果与 运行 在主机上手动给出的结果不同

sshfs with ansible does not give the same result as running it manually on the host

我正在为我的服务器做备份。我为此使用 sshfs。当想要备份文件夹时,备份服务器会要求输入密码。这是我的任务(处理程序)的样子:

- name: Mount backup folder
  become: yes
  expect:
    command: "sshfs -o allow_other,default_permissions {{ backup_server.user }}@{{ backup_server.host }}:/ /mnt/backup"
    echo: yes
    responses:
      (.*)password(.*): "{{ backup_server.password }}"
      (.*)Are you sure you want to continue(.*): "yes"
  listen: mount-backup-folder

它 运行s 并产生这个输出:

changed: [prod1.com] => {
    "changed": true,
    "cmd": "sshfs -o allow_other,default_permissions user@hostname.com:/ /mnt/backup",
    "delta": "0:00:00.455753",
    "end": "2021-01-26 14:57:34.482440",
    "invocation": {
        "module_args": {
            "chdir": null,
            "command": "sshfs -o allow_other,default_permissions user@hostname.com:/ /mnt/backup",        
            "creates": null,
            "echo": true,
            "removes": null,
            "responses": {
                "(.*)Are you sure you want to continue(.*)": "yes",
                "(.*)password(.*)": "password"
            },
            "timeout": 30
        }
    },
    "rc": 0,
    "start": "2021-01-26 14:57:34.026687",
    "stdout": "user@hostname.com's password: ",
    "stdout_lines": [
        "user@hostname.com's password: "
    ]
}

但是当我转到服务器时,文件夹没有与备份服务器同步。但是当我手动 运行 命令时:

sshfs -o allow_other,default_permissions user@hostname.com:/ /mnt/backup

备份确实有效。有人知道这是怎么可能的吗?

我怀疑sshfsSIGHUP杀死了。我对 Ansible 一无所知,所以不知道它是否有官方的方式来忽略 SIGHUP。作为解决方法,您可以这样写:

expect:
  command: bash -c "trap '' HUP; sshfs -o ..."

我安装了 sshfs 并使用 Expect (spawn -ignore HUP ...) and sexpect (spawn -nohup ...) 验证了这个 bash -c "trap ..." 解决方法。我相信它也适用于 Ansible (好像它的 expect 模块使用 Python 的 pexpect.