ERROR 1064 (42000) SQL Syntax error while running ansible playbook - mysql

I am trying to load files into MySQL RDS using ansible-playbook
tasks:
- name: loadBase
block:
- name: copy loadBase files to target
copy: src=../../myApp/{{ item }}
dest=/tmp/{{ item | basename }}
owner=tomcat group=tomcat mode=0600
with_items: "{{ data.loadBase }}"
- name: load loadBase files into target mysql
shell: mysql -h {{ db_host }} -u root -p{{ db_root_password }} {{ db_name }} < /tmp/{{ item | basename }}
with_items: "{{ data.loadBase }}"
no_log: true
- name: cleanup loadBase files on target
file: path=/tmp/{{ item | basename }}
state=absent
with_items: "{{ data.loadBase }}"
when: loadBase
I am using the following config file which is used by the ansible playbook
Config file:
# data files, relative to the source code root, to be
# conditionally run during a deployment
data:
loadBase:
- data/src/main/sql/structure.sql
- data/src/main/sql/data.sql
- data/src/main/sql/update.sql
I see that the .sql files are being added to the /tmp folder in the app server from the ansible server. But I see that the data is not loading to MySQL RDS and I see the following error
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-\nansible_ssh_common_args: '-o ProxyCommand=\"ssh -W %h:%p -q root#bastion-myapp' at line 1",
I am using MySQL RDS Engine version 8.0.11.
Its trying to connect to the bastion host again and its failing?
ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q bastion-app.org"'
Any help, what's wrong here?

Why not using the Ansible MySQL module?
See Insert data into mysql tables using ansible and https://docs.ansible.com/ansible/2.5/modules/mysql_db_module.html:
# Copy database dump file to remote host and restore it to database 'my_db'
- name: Copy database dump file
copy:
src: dump.sql.bz2
dest: /tmp
- name: Restore database
community.mysql.mysql_db:
name: my_db
state: import
target: /tmp/dump.sql.bz2

Related

Command not found when SSHing into server via non-interactive session

I'm using a GitHub action to SSH into my staging server to pull the latest from the repo (which succeeds) then install node modules: yarn (which fails), build the app: yarn build:app (which fails), then restart the app: pm2 restart all (which fails). From what I've read from researching, the commands aren't found because the SSH into the server is a non-interactive session and many things aren't added to $PATH. I have tried adding export PATH="$PATH:/home/***/.nvm/versions/node/v14.5.0/bin/pm2:/home/***/.nvm/versions/node/v14.5.0/bin/pm2" to my script to no avail. I'm still getting command not found.
name: Test Deployment
on:
push:
branches:
- staging
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy Staging
if: github.ref == 'refs/heads/staging'
uses: appleboy/ssh-action#master
with:
host: ${{ secrets.STAGING_SSH_HOST }}
username: ${{ secrets.STAGING_SSH_USERNAME }}
key: ${{ secrets.STAGING_SSH_KEY }}
passphrase: ${{ secrets.STAGING_SSH_PASSPHRASE }}
port: ${{ secrets.STAGING_SSH_PORT }}
script: |
export PATH="$PATH:/home/***/.nvm/versions/node/v14.5.0/bin/pm2:/home/***/.nvm/versions/node/v14.5.0/bin/pm2"
cd ~/***
git pull origin staging
yarn
yarn build:app
pm2 restart all
Receiving errors:
======CMD======
export PATH="$PATH:/home/***/.nvm/versions/node/v14.5.0/bin/pm2:/home/***/.nvm/versions/node/v14.5.0/bin/pm2"
cd ~/***
git pull origin staging
yarn
yarn build:app
pm2 restart all
======END======
err: From github.com:***/***
err: * branch staging -> FETCH_HEAD
err: *** staging -> origin/staging
out: Merge made by the 'recursive' strategy.
2021/04/16 21:28:17 Process exited with status 127
out: .github/workflows/main.yml | 2 +-
out: 1 file changed, 1 insertion(+), 1 deletion(-)
err: bash: line 3: yarn: command not found
err: bash: line 4: yarn: command not found
err: bash: line 5: pm2: command not found
Instead of
export path='$PATH:/home/***/.nvm/versions/node/v14.5.0/bin/pm2:/home/***/.nvm/versions/node/v14.5.0/bin/pm2'
you should try
export PATH="$PATH:/home/***/.nvm/versions/node/v14.5.0/bin/pm2:/home/***/.nvm/versions/node/v14.5.0/bin/pm2"
Notice:
PATH - uppercase
Use double quote instead of single quote to expand the previous value of PATH variable

Vagrant, Ansible and MySql: ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path

I'm dealing with a project in Symfony. It came with a Vagrant file. When I vagrant up, this comes out:
ERROR! no action detected in task. This often indicates a misspelled
module name, or incorrect module path.
The error appears to be in
'/home/chris/Projects/TechAnalyzePlatform/deploy/ansible/roles/db/tasks/mysql.yml':
line 16, column 5, but may be elsewhere in the file depending on the
exact syntax problem.
The offending line appears to be:
# http://ansible.cc/docs/modules.html#mysql-user
- name: update mysql root password for all root accounts
^ here
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
The mysql.yml file contains the following:
---
# MySQL setup
- name: Install MySQL/MariaDB
action: yum name={{ item }}
with_items:
- MySQL-python
- perl-Cache-Cache
- mariadb
- mariadb-server
- name: Start the MySQL service
action: service name=mariadb state=started enabled=yes
# 'localhost' needs to be the last item for idempotency, see
# http://ansible.cc/docs/modules.html#mysql-user
- name: update mysql root password for all root accounts
mysql_user: name=root host={{ item }} password=admin priv=*.*:ALL,GRANT
with_items:
- "{{ ansible_hostname }}"
- 127.0.0.1
- ::1
- localhost
- name: create /.my.cnf
template: src=my.cnf dest=~/.my.cnf
The module exists, what kind of action should I insert there? What can cause this?
Many thanks
Solution: Update Ansible. Solved.

Ansible can't access Database Mysql

It's me again...
I'm learning ansible. I want to deploy a Wordpress server, so I've install mysql correctly. But durring the creation of the Database I've received the following error :
[vm1devops]: FAILED! => {"changed": false, "msg": "unable to find
/home/ERTYerty1234/.my.cnf. Exception message: (1045, \"Access denied
for user 'ERTYerty1234'#'localhost' (using password: NO)\")"}
So I've add the following line :
sudo: True
The database create corretcly, but now when I want to acces on my VM. the Wordpress website can't access the Database. Any idea why ?
Here is how is configure MySQL on my files :
mysql/defaults/main.yml
wp_mysql_db: wordpress
wp_mysql_user: ERTYerty1234
wp_mysql_password: arandompassword
mysql/tasks/main.yml
- name: Create mysql database
mysql_db: name={{ wp_mysql_db }} state=present
#EDIT
- name: Create mysql user
mysql_user:
login_user:{{ wp_mysql_user }}
login_password:{{ wp_mysql_password }}
host:localhost
append_privs:true
priv:'*.*:REQUIRESSL'
host:%
name={{ wp_mysql_user }}
password={{ wp_mysql_password }}
And here are the roles installed on the server :
server/tasks/main.yml
- name: Update apt cache
apt: update_cache=yes cache_valid_time=3600
sudo: yes
- name: Install required software
apt: name={{ item }} state=present
sudo: yes
with_items:
- apache2
- mysql-server
- php7.0-mysql
- php7.0
- libapache2-mod-php7.0
- php7.0-mcrypt
- python-mysqldb
If someone have an idea here. It could be great. THanks a lot.

Import MySQL Dump with Ansible

So im trying to import a MySQL dump that contains two databases with Ansible on a Windows box.
The Ansible playbook should be correct.
- hosts: win
gather_facts: no
tasks:
- name: Import DB
win_command: 'mysql -uroot -p12345678910 -hlocalhost < "C:\sql\dump.sql"'
args:
chdir: C:\Program Files\MySQL\MySQL Server 5.6\bin\
On the windows box if I open CMD and run
cd C:\Program Files\MySQL\MySQL Server 5.6\bin\ and then 'mysql -uroot -p12345678910 -hlocalhost < "C:\sql\dump.sql"' it works perfectly.
Im getting a giant error. What am I doing wrong?
Im able to check the DB with a very similar Playbook:
- hosts: win
gather_facts: no
tasks:
- name: Check DB
win_command: 'mysql -uroot -p12345678910 -e "SHOW DATABASES;"'
args:
chdir: C:\Program Files\MySQL\MySQL Server 5.6\bin\
The problem might be input redirection. Perhaps you can try something like:
win_command: 'Get-Content C:\sql\dump.sql | mysql -uroot -p12345678910 -hlocalhost'
See Redirecting standard input\output in Windows PowerShell

Ansible - How to backup all MySQL databases?

I need to take a backup of all existing MySQL databases on my server with Ansible.
I'm aware of mysql_db module. It takes the names of the databases I'd like to manipulate on one by one, so I must get the list of existing databases before using that module.
Is there any way to backup all MySQL databases at once or to get a list of existing databases with Ansible?
A patch to adds name=all that allows a user to dump or import all data was merged into devel recently, it's not available yet in 1.9.1, but it's already shown in this part of the documentation.
# Dumps all databases to hostname.sql
- mysql_db: state=dump name=all target=/tmp/{{ inventory_hostname }}.sql
Hopefully this will soon be available in a stable release.
(Run sudo pip install ansible --upgrade to upgrade.)
The mysql_db module uses the mysqldump executable under the hood, which in turn provides an --all-databases switch, it's just that the Ansible module does not provide an option to use it via the module.
I would suggest using mysqldump executable via command module for now and in the meantime file a feature request on Ansible's GitHub to add support for it.
Something like this should get you going for now:
- name: Dump all MySQL databases to a single file
command: mysqldump --opt -uroot --all-databases --result-file=/tmp/all-dbs.sql
Adjust the options to mysqldump as desired: http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html
Update Nov 26, 2016:
A patch adding name=all was added to the mysql_db module on May 12, 2015, so the recommended way to dump all databases is:
# Dumps all databases to hostname.sql
- mysql_db: state=dump name=all target=/tmp/{{ inventory_hostname }}.sql
Alternative way, each database in separate file.
---
# This playbook backups all mysql databases into separate files.
- name: backup mysql
vars:
- exclude_db:
- "Database"
- "information_schema"
- "performance_schema"
- "mysql"
tasks:
- name: get db names
shell: 'mysql -u root -p{{ vault_root_passwd }} -e "show databases;" '
register: dblist
- name: backup databases
mysql_db:
state: dump
name: "{{ item }}"
target: "/tmp/{{ item }}.sql"
login_user: root
login_password: "{{ vault_root_passwd }}"
with_items: "{{ dblist.stdout_lines | difference(exclude_db) }}"