# How do I create a MSSQL database user (user on the DB level not the server)

**URL:** <https://forum.ansible.com/t/how-do-i-create-a-mssql-database-user-user-on-the-db-level-not-the-server/36790>\
**Category:** Ansible Project\
**Created:** [June 30, 2022, 6:17pm UTC](https://forum.ansible.com/t/how-do-i-create-a-mssql-database-user-user-on-the-db-level-not-the-server/36790 "2022-06-30T18:17:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adurota\_Gideon](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@Adurota\_Gideon](https://forum.ansible.com/u/Adurota_Gideon)\
**Post date:** [June 30, 2022, 6:17pm UTC](https://forum.ansible.com/t/how-do-i-create-a-mssql-database-user-user-on-the-db-level-not-the-server/36790/1 "2022-06-30T18:17:05Z")

</div>

Hello,  
So I am new to ansible and I have been looking for a way to create a user on mssql database using ansible but I can’t seem to find a module for that. What I saw is more for MySql db.

- name: Create DB user with name ‘bob’ and password ‘12345’ with all database privileges  
mysql\_user:  
name: bob  
password: 12345  
priv: ‘_._:ALL’  
state: present  
Even the above looks like the user will be created on the server not the DB

I have explored using SQLCMD but haven’t found anything concrete on that as well.

Any help is appreciated

---

<div class="post-metadata">

**Author:** ![aidewuado](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@aidewuado](https://forum.ansible.com/u/aidewuado)\
**Post date:** [June 30, 2022, 6:23pm UTC](https://forum.ansible.com/t/how-do-i-create-a-mssql-database-user-user-on-the-db-level-not-the-server/36790/2 "2022-06-30T18:23:15Z")

</div>

Ok,  
Sorry for my english…but  
I unnderstand sqlcmd is for sql server not mysql, try use command with mysql -h and use script sql to create

---

<div class="post-metadata">

**Author:** ![aidewuado](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@aidewuado](https://forum.ansible.com/u/aidewuado)\
**Post date:** [July 1, 2022, 12:45pm UTC](https://forum.ansible.com/t/how-do-i-create-a-mssql-database-user-user-on-the-db-level-not-the-server/36790/3 "2022-07-01T12:45:00Z")

</div>

If you’re trying to create users with Transact SQL, I do it this way.  
You must first have the following prerequisites on the destination host:

- SQLTools instaled (linux or windows)

- have a user with sufficient privileges to create login and SQL users

- In Win, have a user to connect ansible and with sufficient privileges to exec SQLCMD

- In Linux, thee user whit use the public key, with sufficient privileges to exec SQLCMD

- have the transact SQL to create user example:  
USE [master]  
GO  
CREATE LOGIN [user\_SQL] WITH PASSWORD=N’password’, DEFAULT\_DATABASE=[master], CHECK\_EXPIRATION=OFF, CHECK\_POLICY=OFF  
GO  
ALTER SERVER ROLE [sysadmin] ADD MEMBER [user\_SQL]  
GO

- copy your script to destination host

This is the way i use the playbook

**Example win:**

- name: exec createUser  
win\_command: “sqlcmd -U sa -P {{ mssql\_sa\_pwd }} -S {{ ansible\_hostname }} -i script\_createUser.sql”  
args:  
chdir: “{{ servers\_setup\_dir }}”

**Example redhat:**

- name: Ejecuta script de creacion de usuario  
command: “sqlcmd -U sa -P {{ mssql\_sa\_pwd }} -i /var/opt/mssql/create\_user.sql”  
become\_method: su

---

<div class="post-metadata">

**Author:** ![Adurota\_Gideon](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@Adurota\_Gideon](https://forum.ansible.com/u/Adurota_Gideon)\
**Post date:** [July 4, 2022, 5:00am UTC](https://forum.ansible.com/t/how-do-i-create-a-mssql-database-user-user-on-the-db-level-not-the-server/36790/4 "2022-07-04T05:00:43Z")

</div>

Thank you so much for the assistance. I will try this out
