Ansible – Create user with password in Ubuntu/Linux

Ansible is an open-source software platform for configuring and managing computers.

In this article I will explain how to create new user with password.

Ansible has a user module to create a user. You can refer user module docs for more details.

I would like to create a user with name “guest” and password “guest123”.

Ansible will take only encrypted password as input password i.e, if we give passowrd as “guest123” it will decrypt the given password and set, obviosely if we decrypt “guest123” it wont be same as “guest123”.

After deciding username and password generate encrypted password:

python -c ‘import crypt; print crypt.crypt(“guest123”, “guest”)’



In above example I using guest as key to encrypt the password.

After creating password add following code in ansible-playbook:

- hosts: yourhostname
  user: root
  sudo: no

  #Define user and password variables
    # created with:
    # python -c 'python -c 'import crypt; print crypt.crypt("guest123", "guest")'
    password : gu2KmqcJp0Yyo
    user : guest

  # Define task to add user
    - name: add user
      action: user name={{ user }} password={{ password }} update_password=always 
                   shell=/bin/bash home=/home/{{ user }}
        - user

After updating code run above playbook and it will create new user with given password.


Leave a Reply

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

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s