Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

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 [email protected]:/ /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 [email protected]:/ /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": "[email protected]'s password: ",
    "stdout_lines": [
        "[email protected]'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 [email protected]:/ /mnt/backup

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

question from:https://stackoverflow.com/questions/65903216/sshfs-with-ansible-does-not-give-the-same-result-as-running-it-manually-on-the-h

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...