Ansible 中本地和远程主机之间的 SSH 授权

SSH authorization in Ansible between local and remote host

我有一个装有 centos 7 的 Vagrant box,我正在其中创建 LXC 容器。 Vagrant 框中的 Ansible 运行。我用这样的 Ansible 创建容器:

- name: Create containers
  lxc_container:
    name: localdev_nginx
    container_log: true
    template: centos
    container_config:
      - 'lxc.network.ipv4 = 192.168.42.110/24'
      - 'lxc.network.ipv4.gateway = 192.168.42.1'
    container_command: |
        yum -y install openssh-server
        echo "Som*th1ng" | passwd root --stdin
        ssh-keygen -b 2048 -t rsa -f /root/.ssh/id_rsa -q -N ""
    state: started

这是为我创建容器,但在此之后我无法从 Ansible 访问容器。如果我像这样将容器 ssh pubkey 带到 Vagrant known_hosts:

- name: Tell the host about our servers it might want to ssh to
  shell: ssh-keyscan -t rsa 192.168.42.110 >> /root/.ssh/known_hosts

如果我像这样在 Ansible 主机文件中添加容器根密码:

[dev-webservers]
loc-dev-www1.internavenue.com hostname=loc-dev-www1.internavenue.com ansible_ssh_host=192.168.42.110  ansible_connection=ssh  ansible_user=root   ansible_ssh_pass=Som*th1ng

我希望它有更好的解决方案,因为真的很糟糕。怎么才能正常呢?

我将 Vagrant box public 键复制到容器的 authorized_keys 并在主机中使用此标签:

ansible_ssh_extra_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"

只有 Ansible >2.0 才允许这样做