# running a mysql command via ansible to create a user in a mysql docker container

**URL:** <https://forum.ansible.com/t/running-a-mysql-command-via-ansible-to-create-a-user-in-a-mysql-docker-container/32234>\
**Category:** Ansible Project\
**Created:** [February 14, 2020, 10:26am UTC](https://forum.ansible.com/t/running-a-mysql-command-via-ansible-to-create-a-user-in-a-mysql-docker-container/32234 "2020-02-14T10:26:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IP\_Freely](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@IP\_Freely](https://forum.ansible.com/u/IP_Freely)\
**Post date:** [February 14, 2020, 10:26am UTC](https://forum.ansible.com/t/running-a-mysql-command-via-ansible-to-create-a-user-in-a-mysql-docker-container/32234/1 "2020-02-14T10:26:45Z")

</div>

Hi all

Run into a bit of a problem when trying to run this command inside a docker container:

> > **CREATE USER ‘{{ item.user }}’@‘%’ IDENTIFIED BY ‘{{ item.password }}’;**

this is what I’ve done but it’s not working. Does anybody see where I’m going wrong? (where mysql in the example command-line is the container name)

> > **- name: Build the environment**
> 
> > **hosts: server1**
> 
> > **tasks**
> 
> > **- name: create db users via command line**
> 
> > **shell: "docker exec -i mysql sh -c ‘mysql -u root -e "CREATE USER ‘{{ item.user }}’@’%’ IDENTIFIED BY ‘{{ item.password }}’;" ’ "**
> 
> > **with\_items:**
> 
> > **- { user: “user1”, password: “aaaaaaaaa”, db: “db1” }**
> 
> > **- { user: “user2”, password: “bbbbbbbbb”, db: “db2” }**
> 
> > **- { user: “user3”, password: “cccccccccc”, db: “db3” }**

Hope you can point me in the right direction

Regards

John

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 14, 2020, 10:33am UTC](https://forum.ansible.com/t/running-a-mysql-command-via-ansible-to-create-a-user-in-a-mysql-docker-container/32234/2 "2020-02-14T10:33:15Z")

</div>

> Hi all
> 
> Run into a bit of a problem when trying to run this command inside a docker container:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*CREATE USER '{{ item.user }}'@'%' IDENTIFIED BY '{{ item.password }}';\*
> 
> this is what I've done but it's not working. Does anybody see where I'm going wrong? (where mysql in the example  
> command-line is the container name)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*- name: Build the environment\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* hosts: server1\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* tasks\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* - name: create db users via command line\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* shell: "docker exec -i mysql sh -c 'mysql -u root -e \"CREATE USER '{{ item.user }}'@'%' IDENTIFIED BY  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'{{ item.password }}';\" ' "\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* with\_items:\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* - { user: "user1", password: "aaaaaaaaa", db: "db1" }\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* - { user: "user2", password: "bbbbbbbbb", db: "db2" }\*
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* - { user: "user3", password: "cccccccccc", db: "db3" }\*
> 
> Hope you can point me in the right direction

Hello John,

I don't see the point why you want to do this from inside the container. You should be able to use "mysql\_user" module  
with the MySQL port exposed by the docker container.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
