jenkins 当文件名包含空格并用单引号括起来时,rsync / synchronize可能失败

wribegjk  于 2023-01-20  发布在  Jenkins
关注(0)|答案(1)|浏览(195)

建议我在stackoverflow上从Jenkins管道脚本调用ansible-playbook,如下所示:

sh '''
            ansible-playbook /web/playbooks/getfiles.yml -e "dest_host='$dest_host'" -e "dest_user='$dest_user'" -e "source_file_new='$files'" -vv
            '''

运行的Jenkins输出:

+ ansible-playbook /web/playbooks/getfiles.yml -e 'dest_host='\''remhost'\''' -e 'dest_user='\''remuser'\''' -e 'source_file_new='\''/web/3ACF0000316KMFLKO8KG_Letter.pdf
/web/000000BV0000 Insurance - Hambrose .pdf'\''' -vv

下面的剧本工作正常,直到我把文件名放在单引号下,因为文件名有空格:
我尝试了下面的剧本,但他们都失败了,发挥只是冻结。

- name: Copying from "{{ ansible_user }} at "{{ inventory_hostname }}" to this ansible server.
  synchronize:
    src: "'{{ item }}'"
    dest: "{{ playbook_dir }}/tmpfiles/{{ inventory_hostname }}/"
    mode: pull
    copy_links: yes
  with_items:
    - "{{ source_file_new.splitlines() }}"

然后,我尝试使用rsync进行旧方法

- debug:
    msg: "rsync -avzP  {{ ansible_user }}@{{ inventory_hostname }}:'{{ item }}' {{ playbook_dir }}/tmpfiles/{{ inventory_hostname }}/"
  with_items:
    - "{{ source_file_new.splitlines() }}"

- name: Copying from "{{ ansible_user }}" at "{{ inventory_hostname }}" to this ansible server.
  raw: "rsync -avzP  {{ ansible_user }}@{{ inventory_hostname }}:'{{ item }}' {{ playbook_dir }}/tmpfiles/{{ inventory_hostname }}/"
  with_items:
    - "{{ source_file_new.splitlines() }}"

我还尝试使用shellcommand模块。
输出:

TASK [debug] ************************************************************************************************************************************************
task path: /web/playbooks/automation/getfiles/getfiles.yml:115
Tuesday 10 January 2023  10:53:15 -0600 (0:00:00.396)       0:00:03.510 *******
ok: [remhost] => (item=/web/3ACF0000316KMFLKO8KG_Letter.pdf) => {
    "msg": "rsync -avzP  remuser@remhost:'/web/3ACF0000316KMFLKO8KG_Letter.pdf' /web/playbooks/automation/getfiles/tmpfiles/remhost/"
}

ok: [remhost] => (item=/web/000000BV0000 Insurance - Hambrose .pdf) => {
    "msg": "rsync -avzP  remuser@remhost:'/web/000000BV0000 Insurance - Hambrose .pdf' /web/playbooks/automation/getfiles/tmpfiles/remhost/"
}

TASK [Copying from "remuser" at "remhost" to this ansible server.] *************************************************************************************
task path: /web/playbooks/getfiles.yml:120
Tuesday 10 January 2023  10:53:15 -0600 (0:00:00.042)       0:00:03.553 *******
<remhost> ESTABLISH SSH CONNECTION FOR USER: remuser
<remhost> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="remuser"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o 'ControlPath="/home/remuser/.ansible/cp/fcf90ac50d"' -tt remhost 'rsync -avzP  remuser@remhost:'"'"'/web/3ACF0000316KMFLKO8KG_Letter.pdf'"'"' /web/playbooks/automation/getfiles/tmpfiles/remhost/'

当我手动运行debug rsync命令时,它会工作,文件会被传输。
通过在目标主机上创建以下文件并运行行动手册,可以重现此问题。

touch '/web/3ACF0000316KMFLKO8KG_Letter.pdf'
touch '/web/000000BV0000 Insurance - Hambrose .pdf'

我也尝试了ansible.posix.synchronize,但它给我下面的错误:

failed: [remhost] (item=/web/000000BV0000 Insurance - Hambrose .pdf) => {
    "ansible_loop_var": "item",
    "changed": false,
    "cmd": "/bin/rsync --delay-updates -F --compress --copy-links --archive --rsh=/usr/share/centrifydc/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L remuser@remhost:'/web/000000BV0000 Insurance - Hambrose .pdf' /web/playbooks/automation/getfiles/tmpfiles/remhost/",
    "invocation": {
        "module_args": {
            "_local_rsync_password": null,
            "_local_rsync_path": "rsync",
            "_substitute_controller": false,
            "archive": true,
            "checksum": false,
            "compress": true,
            "copy_links": true,
            "delete": false,
            "dest": "/web/playbooks/automation/getfiles/tmpfiles/remhost/",
            "dest_port": null,
            "dirs": false,
            "existing_only": false,
            "group": null,
            "link_dest": null,
            "links": null,
            "mode": "pull",
            "owner": null,
            "partial": false,
            "perms": null,
            "private_key": null,
            "recursive": null,
            "rsync_opts": [],
            "rsync_path": null,
            "rsync_timeout": 0,
            "set_remote_user": true,
            "src": "remuser@remhost:'/web/000000BV0000 Insurance - Hambrose .pdf'",
            "ssh_args": null,
            "ssh_connection_multiplexing": false,
            "times": null,
            "verify_host": false
        }
    },
    "item": "/web/000000BV0000 Insurance - Hambrose .pdf",
    "msg": "Warning: Permanently added 'remhost' (ED25519) to the list of known hosts.\r\n\nThis system is for the use by authorized users only. All data contained\non all systems is owned by the company and may be monitored, intercepted,\nrecorded, read, copied, or captured in any manner and disclosed in any\nmanner, by authorized company personnel. Users (authorized or unauthorized)\nhave no explicit or implicit expectation of privacy. Unauthorized or improper\nuse of this system may result in administrative, disciplinary action, civil\nand criminal penalties. Use of this system by any user, authorized or\nunauthorized, constitutes express consent to this monitoring, interception,\nrecording, reading, copying, or capturing and disclosure.\n\nIF YOU DO NOT CONSENT, LOG OFF NOW.\n\n##################################################################\n# *** This Server is using Centrify                          *** #\n# *** Remember to use your Active Directory account          *** #\n# ***    password when logging in                            *** #\n##################################################################\n\nConnection closed by 192.168.112.2 port 22\r\nrsync: connection unexpectedly closed (0 bytes received so far) [Receiver]\nrsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.2]\n",
    "rc": 255
}
s6fujrry

s6fujrry1#

目标主机上的帐户锁定出现问题。
解锁帐户有助于摆脱上述错误。

相关问题