以下脚本将被复制并在远程主机上运行,即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
#
如果它尝试将此脚本192.168.1.142
从我的清单复制到主机(目前只有一台主机)并在那里运行它,它不起作用。我得到以下输出:
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
一旦我注释掉对的调用sync_w_ctrl
并取消注释,脚本就能正确运行#apt update
。剧本的内容sync-w-ctrl.yml
如下:
- 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
使用 ansible 是否可行,远程主机可以“重用”现有的 ssh 连接来 rsync 某些目录?
问题的约束:
出于安全原因,我无法在控制站上openssh-server
安装并运行它。这就是为什么我希望“重用”由 to建立的 ssh 连接,以便使用 rsync 同步目录。sshd
$ctr_st
192.168.1.142