【发布时间】:2018-11-26 01:51:24
【问题描述】:
我使用 ansible 脚本来加载和启动 https://hub.docker.com/r/rastasheep/ubuntu-sshd/ 容器。
所以它当然开始得很好:
bash-4.4$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8bedbd3b7d88 rastasheep/ubuntu-sshd "/usr/sbin/sshd -D" 37 minutes ago Up 36 minutes 0.0.0.0:49154->22/tcp test
bash-4.4$
所以在 ssh 访问失败后,我从 shell 手动测试 这也可以。
bash-4.4$ ssh root@172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
ECDSA key fingerprint is SHA256:YtTfuoRRR5qStSVA5UuznGamA/dvf+djbIT6Y48IYD0.
ECDSA key fingerprint is MD5:43:3f:41:e9:89:45:06:6f:f6:42:c4:6a:70:37:f8:1d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (ECDSA) to the list of known hosts.
root@172.17.0.2's password:
root@8bedbd3b7d88:~# logout
Connection to 172.17.0.2 closed.
bash-4.4$
所以失败的步骤是尝试从 ansible 脚本中获取并访问 ssh-copy-id
ansible 错误信息是:
Fatal: [172.17.0.2]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.17.0.2' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).\r\n", "unreachable": true}
---
- hosts: 127.0.0.1
tasks:
- name: start docker service
service:
name: docker
state: started
- name: load and start the container we wanna use
docker_container:
name: test
image: rastasheep/ubuntu-sshd
state: started
ports:
- "49154:22"
- name: Wait maximum of 300 seconds for ports to be available
wait_for:
host: 0.0.0.0
port: 49154
state: started
- hosts: 172.17.0.2
vars:
passwordadmin: $6$pbE6yznA$AeFIdI.....K0
passwordroot: $6$TMrxQUxT$I8.JIzR.....TV1
ansible_ssh_extra_args: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
tasks:
- name: Build test container root user rsa ssh-key
shell: docker exec test ssh-keygen -b 2048 -t rsa -f /root/.ssh/id_rsa -q -N ""
所以我什至无法运行构建 ssh 所需的步骤 那怎么办??
- 第一步(ansible任务):加载docker容器
- 2cd 步骤(仅 172.17.0.2 上的 ansible 任务):连接并设置它
- 之后将有第三步在其上运行应用程序。
只有在启动 2cd 步骤时才会出现问题
【问题讨论】:
-
看来问题不在于连接,而在于身份验证(它说“权限被拒绝”)。您是否在清单中或调用 ansible 时提供了 ssh 密码?
-
当然还没有密码 - 我必须首先在远程容器上构建密钥/启动 ssh / 生成密码 --- 在我可以更新任何 ansible localhost ssh 设置信息之前跨度>
-
根据docker镜像的文档,密码是
root。但我没有查看您尝试运行的命令 - 您实际上想要在主机系统上运行的docker命令,因此第二个任务的host必须是127.0.0.1 -
不,我必须在第一个任务完成后在 docker 映像而不是 localhost 上运行
-
是的,
docker exec test负责您基本上说“在名为test的容器中运行以下命令”。但是你必须在宿主的上下文中运行它(因为那是容器运行的地方)。