Generate SSH Keys

I created a simple play to help a non linux team to be able to generate ssh keys on demand. The playbook requires the var email to be set, once done the playbook generates public/private ssh keypair, then emails the contents of the keys to the specified email address. Finally the playbook deletes the generated key pair.


- name: "generate ssh keypair"
  hosts: localhost
    key_name: new_key
  connection: local
    - name: generate keys
      shell: "ssh-keygen -q -f /tmp/{{ key_name }} -N ''"
    - name: set facts
        private_key: "{{ lookup('file', '/tmp/{{ key_name }}'  ) }}"
        public_key: "{{ lookup('file', '/tmp/{{ key_name }}.pub' ) }}"
    - name: email keys
        to: "{{ email }}"
        subject: "New keypair from tower"
        body: "Private: \n {{ private_key }} \n Public: \n {{ public_key }}" 
    - name: cleanup keys
        path: "{{ item }}"
        state: absent
        - "/tmp/{{ key_name }}"
        - "/tmp/{{ key_name }}.pub"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s