Ansible rsync / synchronize失败时,文件名包含空格和单引号



我被建议在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/'

当我手动运行调试rsync命令时,它会工作并且文件被传输。

如果在目标主机上创建以下文件并运行playbook,则可以重现此问题。

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.rnnThis system is for the use by authorized users only. All data containednon all systems is owned by the company and may be monitored, intercepted,nrecorded, read, copied, or captured in any manner and disclosed in anynmanner, by authorized company personnel. Users (authorized or unauthorized)nhave no explicit or implicit expectation of privacy. Unauthorized or impropernuse of this system may result in administrative, disciplinary action, civilnand criminal penalties. Use of this system by any user, authorized ornunauthorized, constitutes express consent to this monitoring, interception,nrecording, reading, copying, or capturing and disclosure.nnIF YOU DO NOT CONSENT, LOG OFF NOW.nn##################################################################n# *** This Server is using Centrify                          *** #n# *** Remember to use your Active Directory account          *** #n# ***    password when logging in                            *** #n##################################################################nnConnection closed by 192.168.112.2 port 22rnrsync: 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
}

问题是目标主机上的帐户锁定。

解锁帐户有助于消除上述错误。

相关内容

  • 没有找到相关文章

最新更新