我尝试了Shell模块的"expect“和"send”功能,代码正在运行,但它没有发送密码,它只是退出了剧本。
---
- hosts: localhost
become: yes
vars:
- remote_password: redhat
tasks:
- name: connecting to a remote host not an ansible slave
shell: |
set timeout 300
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.48.133
expect "password: "
send "{{ remote_password }}\n"
interact
exit 0
args:
executable: /usr/bin/expect输出:
"stdout_lines": [
"spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.48.133",
"/bin/ssh-copy-id: INFO: Source of key(s) to be installed: \"/root/.ssh/id_rsa.pub\"",
"/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed",
"/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys",
"root@192.168.48.133's password: "
]
}不知道为什么这不管用。为什么它不提供密码。
发布于 2021-10-19 09:06:48
您应该使用\r而不是\n,并删除双引号。下面是工作代码。
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.48.133
expect "password: "
send {{ remote_password }}\r
interacthttps://stackoverflow.com/questions/60613201
复制相似问题