I was advised on stackoverflow to call ansible-playbook
from Jenkins pipeline script as below:
sh '''
ansible-playbook /web/playbooks/getfiles.yml -e "dest_host='$dest_host'" -e "dest_user='$dest_user'" -e "source_file_new='$files'" -vv
'''
Jenkins output of the run:
+ 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
The playbook below worked fine until i had put the filenames under single quotes due to filenames having whitespaces:
I tried the below playbooks but they all fail and the play simply freezes.
- 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() }}"
I then tried to go old school with 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() }}"
I also tried to use shell
and command
modules.
Output:
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/'
When i run the debug rsync commands manually it works and the file gets transferred.
This issue is reproducible by creating the below files on target host and running the playbook.
touch '/web/3ACF0000316KMFLKO8KG_Letter.pdf'
touch '/web/000000BV0000 Insurance - Hambrose .pdf'
I also tried ansible.posix.synchronize
but it gives me the below error:
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
}