将 rsa 令牌复制到组内的远程主机

Copy rsa tokens to remote hosts within a group

我正在尝试创建 rsa 并复制到其他远程机器,以便 test[0] 能够通过 ssh 进入 test[1] 和 test[2]

我的库存文件

[ test ]
10.100.0.1
10.100.0.2
10.100.0.3

我想以 root 用户身份在 10.100.0.1 上创建一个 rsa 令牌并将 public 密钥复制到 /home/centos/.ssh/authorized_keys 作为 root 用户到 10.100.0.2 和 10.100 .0.3.

如何使用 ansible 实现这一点。 所有 3 个实例都是 AWS -ec2 centos 7 机器。我希望代码是动态的而不是硬编码的 ips。

我无法继续。

- name: Generate /etc/ssh RSA host key
  command: ssh-keygen -q -t rsa -f /root/.ssh/id_rsa -N ""
    args:
      creates: /root/.ssh/vid_rsa
    run_once: True

理想情况下,您希望将 "master" 实例与其余实例分开,因为并非所有 [test] 机器都是相似的,但值得庆幸的是,可以使用数组表示法来指示第一个实例机.

然后,您只想将 public 密钥作为 "fact" 注入到 test 的第一个成员中,然后在 [= 的所有成员中检索该事实12=].

- hosts: test[0]
  tasks:
  - name: generate ssh key
    command: ssh-keygen etc etc
  - name: grab the public key
    command: cat /root/.ssh/id_rsa.pub
    register: the_pub_key

- hosts: test
  tasks:
  - name: install the public key
    authorized_key:
      key: '{{ hostvars[test0].the_pub_key }}'
      user: root  # or whatever
    vars:
      test0: '{{ groups.test[0] }}'

如果您希望省略 test[0] 本身的 authorized_key,您可以使用适当的 when: 子句来跳过它。

研究 kubespray 的源代码将会发现像这样的各种技巧。