1

I get an "Access denied" error trying to connect to the MySQL Docker container that I start through my ansible script. If I create the container with the Docker CLI tool, everything works. I'm wondering if this has something to do with the environment variables.

This works

$ docker run --name database -e MYSQL_ROOT_PASSWORD=hunter2 -d mysql:5.7

$ mysql -h $CONTAINER_IP_ADDRESS -u root -phunter2
$ mysql>

This is broken

playbook.yml

- name: Start new MySQL container                                                  
  docker:                                                                          
    name: database                                                                   
    image: mysql:5.7                                               
    state: running                                                                 
    env:                                                                           
      MYSQL_ROOT_PASSWORD=hunter2

Then the connection fails:

$ mysql -h $CONTAINER_IP_ADDRESS -u root -phunter2

ERROR 1045 (28000): Access denied for user 'root'@'$CONTAINER_IP_ADDRESS' (using password: YES)

Any idea what I'm missing here?

2 Answers 2

1

The env definition for the docker module should be a dict object, not a string

env:                                                                           
  MYSQL_ROOT_PASSWORD: hunter2
1
  • Sometimes a pair of fresh eyes is all you need. Good catch.
    – izolate
    Nov 27, 2015 at 10:26
1

After some time I found that ansible does not play well with Docker. I've been moving some infraestructure from ansible to Docker, and keeping some legacy code like adding users to mysql or dumping dbs didn't work from Ansible.

The solution is to use shell instead:

- name: Drop/delete mysql table {{ mysql_table }}
  shell: mysqladmin -h 127.0.0.1 -p{{ mysql_password }} -u{{ mysql_user }} drop {{ mysql_table }} -f
  # If it fails it means db was already deleted, so continue.
  ignore_errors: True
  tags:
    - mysql
    - mysql_rebuild_db
    - mysql_rebuild_db_quick

- name: Add mysql table {{ mysql_table }}
  shell: mysqladmin -h 127.0.0.1 -p{{ mysql_password }} -u{{ mysql_user }}     create {{ mysql_table }} -f
  tags:
    - mysql
    - mysql_rebuild_db
    - mysql_rebuild_db_quick

- name: Create mysql user {{ mysql_user }} in {{ mysql_table }}
  shell: echo "CREATE USER {{ mysql_user }}@'localhost' IDENTIFIED BY '{{ mysql_password }}';" | mysql -h 127.0.0.1 -proot -uroot
  # The user may already exists in a previous run
  ignore_errors: True
  tags:
    - mysql
    - mysql_rebuild_db
    - mysql_rebuild_db_quick
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.