Following script is meant to be copied and run on the remote host, i.e. 192.168.1.142
, ansible-playbook -u root -i inventory.ini sync-w-ctrl.yml
:
#!/usr/bin/bash
au=jimmy
dirs=(
"/home/${au}/Music"
)
ctr_st="192.168.1.112"
sync_w_ctrl() {
for d in ${dirs[@]}; do
rsync -e ssh -av --delete --no-o --no-g --chown=oumaima:oumaima "root@${ctr_st}:${d}" /home/oumaima/Music
done
}
sync_w_ctrl
#apt update
#
If it try to copy this script to the hosts (just one host for now, 192.168.1.142
) from my inventory and run it there it does not work. I get the following output:
PLAY [Run Bash script on all machines with multiple file arguments] *************************************
TASK [Copy and run a script that syncs directories] *****************************************************
fatal: [192.168.1.142]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Shared connection to 192.168.1.142 closed.", "unreachable": true}
PLAY RECAP **********************************************************************************************
192.168.1.142 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
The script does run correctly once I comment out the call to sync_w_ctrl
and uncomment #apt update
.
The content of the playbook, sync-w-ctrl.yml
is:
- name: Run Bash script on all machines with multiple file arguments
hosts: myhosts
gather_facts: false
vars:
au: "jimmy"
tasks:
- name: Copy and run a script that syncs directories
script: /home/{{ au }}/CS/SoftwareDevelopment/MySoftware/Bash/ansible/sync-w-ansible.sh
Is it at all doable with ansible, for a remote host to ‘reuse’ the existing ssh connection in order to rsync some directories?
Constraints of the problem:
For security reasons, I cannot have openssh-server
installed and sshd
running on my control station. That’s why I was hoping to ‘reuse’ the ssh connection established by $ctr_st
to 192.168.1.142
in order to sync directories with rsync.