Sitemap
Nitin Namdev

Nitin Namdev

Certified RHEL Administrator who can help you in linux automation tasks

Create Multiple Users with ansible

2 min readNov 21, 2022

In the past couple of months DevOps is on its high and daily increasing the demand of the automation tools like ansible, terraform and bash script.

So I am going to present you a demo of how you can create multiple users with password, and the password will be same as the user and little bit text that you can edit in the file

For this I create an inventory for manage all the hosts. In this file you can define your all the hosts that are need to be configure. You can give the any name to your inventory, here I give the all inside the inventory directory.

Inventory file (inventory/all)—

[testmachines]
10.206.68.13

[testmachines:vars]
ansible_connection=ssh
ansible_user=root
ansible_ssh_pass=newpass!@

This is the simple playbook file for create new users via ansible, you can give the list of users inside the variable section as follows for multiple user

Get Nitin Namdev’s stories in your inbox

Join Medium for free to get updates from this writer.

create-users.yml —

- hosts: testmachines
vars:
default_shell: /bin/bash
isRootAccess: true
users:
- nitin
- prateek
- vivek
- sumit
tasks:
- name: Add the user with a bash shell, appending the group 'root' with password
user:
name: "{{ item }}"
shell: "{{ default_shell }}"
group: "{{ 'root' if isRootAccess else item }}"
password: "{{ item + 'ansible' | password_hash('sha512') }}"
with_items: "{{ users }}"

This playbook will create multiple users dynamically that are inside the users variable also the user password will be the usernameansible.

like for user nitin the password will be nitinansible. And the default shell will be /bin/bash for all the users you can also change it to any other shell like nologin, sh, zsh

You can run it via the following command in terminal

ansible-playbook create-users.yml -i inventory/all

In the above comand pass the inventory file using the -i parameter followed by the name of the inventory file.

Thank you for reading. if you have any queries you can reach me at @

--

--

Nitin Namdev
Nitin Namdev

Written by Nitin Namdev

Certified RHEL Administrator who can help you in linux automation tasks