Reputation: 6086
I'm new to Ansible and I'm struggeling with creating a new user on a remote machine and copying ssh-keys (for git) from the local machine to the remote machine's new user.
Basically, from localmachine/somepath/keys/
to remotemachine/newuser/home/.ssh/
.
So far I tried:
- name: Create user
hosts: remote_host
remote_user: root
tasks:
- name: Create new user
user: name=newuser ssh_key_file=../keys/newuser
While this creates the newuser on the remote machine, it doesn't copy any keys (.ssh is still empty). I also tried authorized_key
as a second task but only got an error message when trying to copy the private key.
Is it even possible that the keys are still added after I already ran it and newuser
already exists. Ie, can I just run it again or will I have to delete the newuser first?
Upvotes: 2
Views: 14775
Reputation: 11615
The ssh_key_file
is the path used by the option generate_ssh_key
of user
module. It's not the path of a local SSH key to upload to the remote user created.
If you want to upload the SSH key, you have to use the copy
module
- name: Create user
hosts: remote_host
remote_user: root
tasks:
- name: Create new user
user:
name: newuser
- name: Create .ssh folder
file:
path: ~newuser/.ssh
state: directory
owner: newuser
group: newuser
mode: 0700
- name: Upload SSH key
copy:
src: ../keys/newuser
dest: ~newuser/.ssh/id_rsa
owner: newuser
group: newuser
mode: 0700
BTW, it's recommended to use the YAML syntax instead of the args syntax.
Upvotes: 6