ansiblesshfs

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


I am working on backups for my server. I am using sshfs for this. When wanting to back up a folder the backup server asks for a password. This is what my task (handler) looks like:

- 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

It runs and produces this output:

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: "
    ]
}

But when I go to the server the folder is not synced with the backup server. BUT when I run the command manually:

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

The backup DOES work. Does anybody know how this is possible?


Solution

  • I suspect sshfs was killed by SIGHUP. I know nothing about Ansible so don't know if it has the official way to ignore SIGHUP. As a workaround you can write like this:

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

    I installed sshfs and verified this bash -c "trap ..." workaround with Expect (spawn -ignore HUP ...) and sexpect (spawn -nohup ...). I believe it'd also work with Ansible (seems like its expect module uses Python's pexpect).