首页 > 解决方案 > Ansible 使用密码配置跳转主机

问题描述

我有三个主机 - A、B 和 C,其中 A 是我的 Ansible 主机,C 是需要通过 B 访问的目标主机。

作为我的清单文件的一部分,我拥有 B 和 C 主机的详细信息,如下所示:

    b:
      ansible_host: "{{b_ip}}"
      ansible_port: 22
      ansible_user: root
      ansible_password: password
    c:
      ansible_host: "{{c_ip}}"
      ansible_port: 22
      ansible_user: root
      ansible_password: password
      ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q root@{{b_ip}}"'

我们如何配置 ansible 以使用清单文件中 B 和 C 主机中提到的密码,而不是使用 ~/.ssh/config 文件中的任何其他配置?

标签: sshansible

解决方案


没有直接的方法可以将跳转主机的密码作为 ProxyCommand 的一部分提供。

所以,我最终做了以下事情:

# Generate SSH keys for the Ansible Host A

- hosts: localhost
  become: false
  tasks:
    - name: Generate the localhost ssh keys
      openssh_keypair:
        path: ~/.ssh/id_rsa
        force: no

# Copy the host keys of Ansible host to the jump host into the authorized keys of # the ssh to ensure that no password is prompted while logging into jump host A

- hosts: b
  become: false
  tasks:
    - name: Create ssh directory if not exists
      file:
        path: /home/root/.ssh
        state: directory

    - name: Copy SSH keys into host B
      copy:
        src: ~/.ssh/id_rsa.pub
        dest: /home/root/.ssh/authorized_keys
        force: yes

推荐阅读