To make sure that host A can connect to the database of the host B, I try to use mysql_db on a remote host
- 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
and I get that error message even if the login/pass is right
msg: unable to connect, check login_user and login_password are correct,
or alternatively check ~/.my.cnf contains credentials
Am i missing something? can I set login_host with a specific ansible host?
You can use a stat command in the (default) option batch abort mode to query file attributes. If the file does not exist, the stat command fails and so does the script. Then, test WinSCP exit code to determine, if the file exists or not.
Testing Connectivity to Nodes To test that Ansible is able to connect and run commands and playbooks on your nodes, you can use the following command: ansible all -m ping.
The easiest way to check if a file exists using Ansible is with the stat module. The purpose of the stat module is to retrieve facts about files and folders and record them in a register.
In Linux, the stat command is a command-line utility that provides detailed information about filesystems or files. Ansible, considered the most diverse and popular automation tool, provides a module for fetching file and file system information as native Linux stat command.
Did you configure the mysql to accept the connection from Host A because
by default mysql only accept connection from localhost.
If you have configured that the mysql accept the connection from Host A then you can verify that the database exist
- 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 the B_database exist
- 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 true and you are still getting error, then please do this:
Add this task inside your task/main.yml
- name: Copy the root credentials as .my.cnf file
template:
src: root.cnf.j2
dest: "~/.my.cnf"
mode: 0600
and this will be your root.cnf.j2
[client]
user=root
password={{ password }}
What it will do is, to connect the mysql from the root user without password and perform these task. You can remove it after running all the task or leave it like this because it is under root and have tight permission.
In my situation I only wanted to import an SQL file once, when the database gets created. Here's how to achieve that, the Ansible way:
---
- name: Test playbook
hosts: localhost
connection: local
tasks:
- name: Database exists
mysql_db:
name: some_db
state: present
register: database_exists
- name: Import database schema
mysql_db:
name: some_db
state: import
target: database.sql.gz
when: database_exists.changed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With