简体   繁体   中英

deploying SSH keys to servers

I have the following playbook:

---
 - 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') }}"

I have a local directory that contains both my ssh public and private keys like this:

./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

I need to create some playbooks that can be used to push the keys to various hosts defined in the hosts fields.

However, the .ssh key directories are getting created incorrectly. ie a directory called tom gets created below an id_rsa and id_rsa.pub directory.

eg

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

Does anybody have a good example playbook for this ?

Try:

 ---
 - 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

You can copy the whole directory tom or the content of tom with 'tom/'. Also you don't need to specify the files, when you copy the complete directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM