Did you configure the mysql to accept the connection from Host A because
by default mysql only accept connection from localhost.
If you configured that MySQL accepts a connection from host A, then you can verify that the database exists
- name: check if DB exists
shell: mysql -e 'SHOW DATABASES;' | grep {{ B_database }}
register: dbstatus
failed_when: dbstatus.rc == 2
Then you can run your task if database B exists
- name: Make sure A can connect to B database
mysql_db:
login_user=root
login_password=password
login_host=B_address
login_port=B_port
name=B_database
state=present
when: dbstatus.rc == 0
no_log: yes # You can disable this, if you want to print the stdout
If you are sure that the above cases are correct and still get an error, do the following:
/main.yml
- name: Copy the root credentials as .my.cnf file
template:
src: root.cnf.j2
dest: "~/.my.cnf"
mode: 0600
root.cnf.j2
[client]
user=root
password={{ password }}
, mysql root . , root .