Execute an .sql script on mysql with secure installation

Hi,
I am trying to use an sql script to create a database in mariadb on a openbsd server with ansible.
My playbook first create the script with the name of the database to create and then uses expect to give the command “mysql -u root -p < script.sql” and then responses the password with “” because theres no password.
I try to use the script created by ansible and it works.
I try to use the same command that ansible is using and it works.
I took the “< script.sql” part to see if the problem was the password, and ansible enter in mysql without returning any error.

When i use the playbook with “< script.sql” in command, ansible returns the error of non-zero return code returning rc=1.

Anyone know how to solve it??

I’ve tried also the community.mysql module, but it didnt worked also to create the first database.

Best Regards,

Tiago Oliveira

Hi,
I am trying to use an sql script to create a database in mariadb on a openbsd server with ansible.
My playbook first create the script with the name of the database to create and then uses expect to give the command "mysql -u root -p < script.sql" and then responses the password with "" because theres no password.
I try to use the script created by ansible and it works.
I try to use the same command that ansible is using and it works.
I took the "< script.sql" part to see if the problem was the password, and ansible enter in mysql without returning any error.

When i use the playbook with "< script.sql" in command, ansible returns the error of non-zero return code returning rc=1.

Anyone know how to solve it??

Maybe you start with sharing your playbook and the contents of the script!?

The modules mysql_user and mysql_db should be able to create your database with the associated users.

Regards
         Racke

Yeah, sorry.

  • name: Create a new .sql file for the table ‘{{table_name}}’
    template:
    src: templates/new_table_template
    dest: /home/secnet/{{table_name}}.sql
    expect:
    command: mysql -u root -p < {{table_name}}.sql
    responses:
    ‘Enter password:’: ‘’
    register: wp
    failed_when: wp.rc not in [ 0, 1 ]

The mysql_db module cant enter in mysql, returns the error “msg”: “unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has
the credentials. Exception message: (1698, "Access denied for user ‘root’@‘localhost’")”
}

since i need to enter with root user.

Regards,

Tiago

A segunda-feira, 20 de dezembro de 2021 à(s) 17:14:36 UTC, ra...@linuxia.de escreveu:

oops, wrong code.

  • name: Create a new .sql file for the table ‘{{table_name}}’
    template:
    src: templates/new_table_template
    dest: /home/secnet/{{table_name}}.sql

  • name: Create the new database with name ‘{{table_name}}’
    expect:
    command: mysql -u root -p < {{table_name}}.sql
    responses:
    ‘Enter password:’: ‘’
    register: wp
    failed_when: wp.rc not in [ 0, 1 ]

This one is the right one

A segunda-feira, 20 de dezembro de 2021 à(s) 17:28:50 UTC, Tiago Oliveira escreveu:

oops, wrong code.

- name: Create a new .sql file for the table '{{table_name}}'
template:
src: templates/new_table_template
dest: /home/secnet/{{table_name}}.sql

- name: Create the new database with name '{{table_name}}'
expect:
command: mysql -u root -p < {{table_name}}.sql
responses:
'Enter password:': ''
register: wp
failed_when: wp.rc not in [ 0, 1 ]

This one is the right one

From expect module documentation:

-- snip --
If you want to run a command through the shell (say you are using <, >, |, and so on), you must specify a shell in the command such as /bin/bash -c "/path/to/something | grep else".
-- snap --

Don't understand why you try to feed an empty password to MySQL. Do you use become: yes in your playbook? This would execute the task as root user.

Regards
           Racke

So, why don't you simply omit the "-p" to avoid being prompted for the non-
existent password?

Antony.