Ansible: Add user to a server
Add users, setting groups is fairly straight-forward for ansible. However when we get to setting the password, it becomes tricky.
Setting User Password
Generating a hashed password for a unix server can be tricky. This is because of the compatibility of hashing algorithms on different platforms. Consider generating the hashed password on the target machine.
# created with: # python -c 'import crypt; print crypt.crypt("This is my Password", "$1$SomeSalt$")'
- name: Create user user: name=user shell=/bin/bash home=/srv/user groups=admin,sudo generate_ssh_key=yes ssh_key_bits=2048 - name: Set password to user shell: echo user:plain_text_password | sudo chpasswd no_log: True
- name: "Set user password: someuser" command: 'echo "somepassword"| passwd --stdin "someuser"' sudo: yes