将SSH密钥部署到服务器

时间:2016-04-26 20:58:44

标签: ssh ansible ssh-keys

我有以下剧本:

---
 - name: provision toms keys
    hosts: ssh4.demo.com
    tasks:
     - name: ensure user tom is present
       user: name=tom state=present

     - name: ensure private key and public one are present
       copy: src=ssh_keys/tom dest=/.ssh mode=0600
       with_items:
         - id_rsa.pub
         - id_rsa

 - name: provision toms public keys
    hosts: ssh1.demo.com

    sudo: yes

    tasks:

     - user: name=tom comment="Add tom" group=staff

     - name: Placing key
       authorized_key: user=tom key="{{ lookup('file', 'ssh_keys/tom/id_rsa.pub') }}"

我有一个包含我的ssh公钥和私钥的本地目录,如下所示:

./ssh_keys
./ssh_keys/david
./ssh_keys/david/id_rsa
./ssh_keys/david/id_rsa.pub
./ssh_keys/fred
./ssh_keys/fred/id_rsa
./ssh_keys/fred/id_rsa.pub
./ssh_keys/joe
./ssh_keys/joe/id_rsa
./ssh_keys/joe/id_rsa.pub
./ssh_keys/paul
./ssh_keys/paul/id_rsa
./ssh_keys/paul/id_rsa.pub
./ssh_keys/peter
./ssh_keys/peter/id_rsa
./ssh_keys/peter/id_rsa.pub
./ssh_keys/tom
./ssh_keys/tom/id_rsa
./ssh_keys/tom/id_rsa.pub

我需要创建一些可用于将密钥推送到主机字段中定义的各种主机的剧本。

但是,.ssh密钥目录的创建不正确。即在id_rsa和id_rsa.pub目录下创建名为tom的目录。

e.g。

 /home/tom/.ssh/id_rsa/tom/id_rsa

有人为此有一个很好的示例剧本吗?

1 个答案:

答案 0 :(得分:3)

尝试:

 ---
 - name: provision toms keys
    hosts: ssh4.demo.com
    tasks:
     - name: ensure user tom is present
       user: name=tom state=present

     - name: ensure private key and public one are present
       copy: src=ssh_keys/tom/ dest=/.ssh mode=0600

您可以使用' tom /'复制整个目录tom或tom的内容。 当您复制完整目录时,也不需要指定文件。