hain
hain

Reputation: 31

Key Generation Using Ansible

I have a question,whether it is possible to create SSH key without storing locally,instead of i can save it as a variable.Whether is there any option so that i can have my keys safely without saving locally.

Thanks

Upvotes: 1

Views: 973

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

It is a bit hacky, but answers the question:

---
- hosts: localhost
  tasks:
    - expect:
        command: ssh-keygen -b 2048 -t rsa -f /dev/stdout -q
        responses:
            Overwrite: y
            Enter: "\n"
      failed_when: "'BEGIN RSA PRIVATE KEY' not in priv_key_raw.stdout"
      changed_when: false
      register: priv_key_raw

    - set_fact:
        priv_key: "{{ (priv_key_raw.stdout_lines[3:])[:-1] }}"

    - shell: "/bin/echo -e \"{{ priv_key | join('\\n') }}\" | ssh-keygen -y -f /dev/stdin"
      register: pub_key_raw
      changed_when: false

    - set_fact:
        pub_key: "{{ pub_key_raw.stdout }}"

    - debug: var=priv_key
    - debug: var=pub_key

Upvotes: 2

Related Questions