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
  vars:
    key_name: new_key
    email: 
  connection: local
  tasks:
    - name: generate keys
      shell: "ssh-keygen -q -f /tmp/{{ key_name }} -N ''"
    - name: set facts
      set_fact:
        private_key: "{{ lookup('file', '/tmp/{{ key_name }}'  ) }}"
        public_key: "{{ lookup('file', '/tmp/{{ key_name }}.pub' ) }}"
    - name: email keys
      mail:
        to: "{{ email }}"
        subject: "New keypair from tower"
        body: "Private: \n {{ private_key }} \n Public: \n {{ public_key }}" 
    - name: cleanup keys
      file:
        path: "{{ item }}"
        state: absent
      with_items:
        - "/tmp/{{ key_name }}"
        - "/tmp/{{ key_name }}.pub"

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s