How to add an existing public key to authorized_keys file using Ansible and user module?

Alexandre Roux

I'm programming a simple task with Ansible to create a user and add an existing RSA public key. This is the code I wrote:

- name: SYSTEM - Create test user
  tags: system-user
  user: 
        name: "{{ test_user }}"
        state: present
        createhome: yes

- name: SYSTEM - Add existing pub key for test user
  tags: system-user
  copy: 
       content: "{{ test_user_pubkey }}"
       dest: "/tmp/test_user_id_rsa.pub"
       force: no
       owner: "{{ test_user }}"
       group: "{{ test_user }}"
       mode: 0600

- name: SYSTEM - Set authorized key for test_user took from file
  tags: system-user
  authorized_key:
        user: "{{ test_user }}"
        state: present
        key: "{{ lookup('file', '/tmp/test_user_id_rsa.pub') }}"

The code I wrote is not elegant and I think that the best option will be to add the existing RSA public key with the user creation block in order to create and filled up the authorized_keys file.

I've read the Ansible user module but ssh_key_file method does not include the possibility to echo the value of an existing pub key to the authorized_keys file (the end purpose is to be able to remote connect with ssh using the user and the private key).

ssh_key_file = Optionally specify the SSH key filename. If this is a relative filename then it will be relative to the user's home directory.

Is it possible with Ansible to manage this process within the user module?

techraf

The answer to your problem is:

- name: SYSTEM - Create test user
  tags: system-user
  user: 
    name: "{{ test_user }}"
    state: present
    createhome: yes

- name: SYSTEM - Set authorized key for test_user took from file
  tags: system-user
  authorized_key:
    user: "{{ test_user }}"
    state: present
    key: "{{ test_user_pubkey }}"

That's all that is needed.


Regarding your reading of the documentation, ssh_key_file pertains to generating an SSH key pair, which is not what you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add a public ssh key to the authorized_keys of a user

Gitlab CE Doesn't Add a Public Key to authorized_keys

Using public key from authorized_keys with Java security

Public key authentication for LDAP users using local authorized_keys

Assign multiple public ssh keys to user definitions with authorized_key module in Ansible

Making an alias to add key to remote authorized_keys file

How do I add SSH Keys to authorized_keys file?

Ansible update authorized_keys file

How to use multiple SSH keys in an `authorized_keys` file for a user on my server?

How to secure ~/.ssh/authorized_keys file?

Can't ssh even with public key added to authorized_keys

Remove authorized_keys using Ansible for multiple keys and multiple users

Create and then edit file in Ansible problems (authorized_keys)

Generate a DSA key pair with 2048 bit - add to the authorized_keys

I accidentally did ssh-add -d .. how do I re-add my key from authorized_keys?

ansible - copy key to authorized keys file

Appending another key generated in PuTTYgen to authorized_keys file

Add a value to an existing key in Ansible

How to truncate an existing and opened file using Ansible?

Problem with authorized_keys with ansible

How to verify file using rsa public key

After changing permission of ~/.ssh/authorized_keys and the ~/.ssh directory I get a permission denied (public key)

Adding a public key to ~/.ssh/authorized_keys does not log me in automatically

Unable to connect to AWS instance even after manually adding in public key to authorized_keys

How to load a RSA public key using Python's cryptography module

how to add lines to existing file using python

Java - Encrypt String with existing public key file

Add a new key-value to a json file using Ansible

Add a key to existing json file

TOP Ranking

HotTag

Archive