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

455 views
Skip to first unread message

John Simmons

unread,
Feb 14, 2020, 5:26:45 AM2/14/20
to Ansible Project
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 

Stefan Hornburg (Racke)

unread,
Feb 14, 2020, 5:33:46 AM2/14/20
to ansible...@googlegroups.com
On 2/14/20 11:26 AM, John Simmons wrote:
> 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

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
Racke

>
> Regards
>
> John 
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/dcfffe3b-cb6f-4e4a-a85c-abbd5ff2b7be%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/dcfffe3b-cb6f-4e4a-a85c-abbd5ff2b7be%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
0 new messages