2017-09-14 77 views
0

我已經創建了一個使用ansible的用戶,現在想將.ssh/id_rsa.pub文件複製到authorized_keys文件中。ansible - 將密鑰複製到授權密鑰文件

我檢查了authorized_keys模塊,但用於將主機上的密鑰複製到guest虛擬機。

想知道什麼是正確的做法。

- name: Adding user - {{ user }} 
    user: name={{ user }} 
     group={{ group }} 
     shell=/bin/bash 
     password=${password} 
     groups=sudo 
     append=yes 
     generate_ssh_key=yes 
     ssh_key_bits=2048 
     ssh_key_file=.ssh/id_rsa 

回答

0

生成的密鑰由user模塊返回,這樣你就可以register的結果,然後在隨後的authorized_key任務中使用的關鍵。也就是說,如果我有這樣的一個劇本:

- hosts: localhost 
    tasks: 
    - name: add user 
     user: 
     name: testuser 
     shell: /bin/bash 
     password: secret 
     append: yes 
     generate_ssh_key: yes 
     ssh_key_bits: 2048 
     register: newuser 

    - debug: 
     var: newuser 

我將看到輸出類似:

TASK [debug] ******************************************************************* 
ok: [localhost] => { 
    "newuser": { 
     "append": true, 
     "changed": true, 
     "comment": "", 
     "group": 21946, 
     "home": "/home/testuser", 
     "move_home": false, 
     "name": "testuser", 
     "password": "NOT_LOGGING_PASSWORD", 
     "shell": "/bin/bash", 
     "ssh_fingerprint": "2048 SHA256:Tn6UOl/WYToJCaW3QUnLMWgEfthILIsoCP+534qWzfw ansible-generated on lkellogg-pc0dzzve (RSA)", 
     "ssh_key_file": "/home/testuser/.ssh/id_rsa", 
     "ssh_public_key": "ssh-rsa ... ansible-generated on examplehost", 
     "state": "present", 
     "uid": 21940 
    } 
} 

所以,你可以添加這樣一個任務:

- authorized_key: 
    user: root 
    state: present 
    key: "{{ newuser.ssh_public_key }}"