Ansible double quotes and variables [duplicate] - mysql

This question already has answers here:
Ansible Command module says that '|' is illegal character
(2 answers)
Closed 4 years ago.
I'm trying implement a solution I've found on stackoverflow for truncating all tables in a database.
It works when I run it on terminal command line but unfortunately it does not work on Ansible.
Problematic part is this -e \"truncate table $table\" How should I handle a variable in double quote ? I couldn't found any solution so far.
- set_fact: db_truncate_command="mysql -v -h {{ wp_db_host }} -u {{ wp_db_user }} -p{{ wp_db_password }} -Nse 'show tables' {{ wp_db_name }} | while read table; do mysql -v -h {{ wp_db_host }} -u {{ wp_db_user }} -p{{ wp_db_password }} -e \"truncate table $table\" {{ wp_db_name }}; done"
when: stat_db_file.stat.exists
- debug:
msg: "command {{ db_truncate_command }} "
- name: Truncate existing tables in db
command: >
{{ db_truncate_command }}
when: stat_db_file.stat.exists
The output I get:
TASK [mysql : set_fact] ***********************************************************************************************************************************************************
ok: [ansible.mydomain.com] => {"ansible_facts": {"db_truncate_command": "mysql -v -h 192.155.190.255 -u wp_user -pMyPassword -Nse 'show tables' ansible | while read table; do mysql -v -h 192.155.190.255 -u wp_user -pMyPassword -e \"truncate table $table\" ansible; done"}, "changed": false}
TASK [mysql : debug] **************************************************************************************************************************************************************
ok: [ansible.mydomain.com] => {
"msg": "command mysql -v -h 192.155.190.255 -u wp_user -pMyPassword -Nse 'show tables' ansible | while read table; do mysql -v -h 192.155.190.255 -u wp_user -pMyPassword -e \"truncate table $table\" ansible; done "
}
TASK [mysql : Truncate existing tables in db] *************************************************************************************************************************************
fatal: [ansible.mydomain.com]: FAILED! => {"changed": true, "cmd": ["mysql", "-v", "-h", "192.155.190.255", "-u", "wp_user", "-pMyPassword", "-Nse", "show tables", "ansible", "|", "while", "read", "table;", "do", "mysql", "-v", "-h", "192.155.190.255", "-u", "wp_user", "-pMyPassword", "-e", "truncate table $table", "ansible;", "done"], "delta": "0:00:00.005262", "end": "2018-02-24 10:33:22.808490", "msg": "non-zero return code", "rc": 1, "start": "2018-02-24 10:33:22.803228", "stderr": "mysql: [Warning] Using a password on the command line interface can be insecure.", "stderr_lines": ["mysql: [Warning] Using a password on the command line interface can be insecure."], "stdout": "mysql Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using EditLine wrapper\n
Added output of fail message

Using shell instead of command works.
- name: Truncate existing tables in db
shell: >
{{ db_truncate_command }}
when: stat_db_file.stat.exists

Related

GitHub Actions for Automatic Tag Not Working

One of my GitHub Actions for automatic tagging is not working and I don't seem to know why.
Here is my tag.yml:
name: 'tag'
on:
push:
branches:
- main
jobs:
tag:
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout#v2.4.0
- name: 'Tag'
uses: anothrNick/github-tag-action#1.36.0
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
The error I get is this:
Warning: Unexpected input(s) 'repo-token', valid inputs are ['entryPoint', 'args']
Run anothrNick/github-tag-action#1.36.0
/usr/bin/docker run --name a72c5b92e429db40e09e9b93f3e458fdb9_f74ce8 --label 9916a7 --workdir /github/workspace --rm -e INPUT_REPO-TOKEN -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RETENTION_DAYS -e GITHUB_RUN_ATTEMPT -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_REF_NAME -e GITHUB_REF_PROTECTED -e GITHUB_REF_TYPE -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e GITHUB_ACTION_REPOSITORY -e GITHUB_ACTION_REF -e GITHUB_PATH -e GITHUB_ENV -e RUNNER_OS -e RUNNER_ARCH -e RUNNER_NAME -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/terraform-provider-mirantis/terraform-provider-mirantis":"/github/workspace" 9916a7:2c5b92e429db40e09e9b93f3e458fdb9
*** CONFIGURATION ***
DEFAULT_BUMP: minor
WITH_V: false
RELEASE_BRANCHES: master,main
CUSTOM_TAG:
SOURCE: .
DRY_RUN: false
INITIAL_VERSION: 0.0.0
TAG_CONTEXT: repo
PRERELEASE_SUFFIX: beta
VERBOSE: true
Is master a match for main
Is main a match for main
pre_release = false
From https://github.com/Richard-Barrett/terraform-provider-mirantis
* [new tag] v1.0-beta -> v1.0-beta
fatal: ambiguous argument '0.0.0': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Merge pull request #14 from Richard-Barrett/Richard-Barrett-patch6 automating terraform with release and goreleaser
minor
Bumping tag 0.0.0.
New tag 0.1.0
2022-01-16T04:39:36Z: **pushing tag 0.1.0 to repo Richard-Barrett/terraform-provider-mirantis
"message": "Bad credentials",
"documentation_url": "https://docs.github.com/rest"
}
Error: Tag was not created properly.
Here is the public Repo it is affiliated with: https://github.com/Richard-Barrett/terraform-provider-mirantis
Any advice...?
You are missusing this action, it should be:
- name: 'Tag'
uses: anothrNick/github-tag-action#1.36.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
All parameters are passed by ENV as described here

How to use variable instead of query in MySQL command

I am using the below command to get data from a database;
mysql --ssl -Ns -h {{ db_addr }} -t -u {{ db_user }} -p'{{ db_password }}' database_name -e "'describe table22;'"
But now I am getting describe table22; inside variable db_query
and I am not sure how to use this variable db_query inside the same command.
I am trying below command but not getting the required output.
mysql --ssl -Ns -h {{ db_addr }} -t -u {{ db_user }} -p'{{ db_password }}' database_name -e "$db_query"
mysql --ssl -Ns -h {{ db_addr }} -t -u {{ db_user }} -p'{{ db_password }}' identityiq -e "{{ db_query }}"
Is working fine for me.
So db_query is the variable collecting sql query from the user and we are passing it in the command to get the output.

Ansible playbook gets stuck trying to perform mysql operations

I'm trying to perfrom a simple "SHOW DATABASES;" command with an ansible playbook and it gets stuck when executes the command.
I tried different options
- hosts: servers
become: true
vars_prompt:
- name: "db_passw"
prompt: "DB root password?"
tasks:
- name: Configure Database
shell: |
mysql -u root -p {{ db_passw }} < ~/sql_query.sql
Also
- hosts: servers
become: true
vars_prompt:
- name: "db_passw"
prompt: "DB root password?"
tasks:
- name: Configure Database
shell: |
mysql -u root -p {{ db_passw }} -e "SHOW DATABASES;"
And I also tried, just as a test, to put the password explicitly
- hosts: servers
become: true
vars_prompt:
- name: "db_passw"
prompt: "DB root password?"
tasks:
- name: Configure Database
shell: |
mysql -u root -p <root_passw> -e "SHOW DATABASES;"
But it always gets stuck at the same point. I tried to execute above's mysql commands in the remote machine shell and they work with no problems. I also tried to execute other commands before mysql's ones and they are being executed.
Is there any problem between ansible and mysql?
I know thta there is a MySQL module for Ansible, but it's functionality is too limited.
Thank you
Works for me. The play below
- hosts: dbserver
tasks:
- command: |
mysql -e "SHOW DATABASES;"
register: result
- debug:
var: result
gives:
TASK [debug]
ok: [dbserver] => {
"result": {
"changed": true,
"cmd": [
"mysql",
"-e",
"SHOW DATABASES;"
],
"delta": "0:00:00.010958",
"end": "2019-04-04 16:19:58.359100",
"failed": false,
"rc": 0,
"start": "2019-04-04 16:19:58.348142",
"stderr": "",
"stderr_lines": [],
"stdout":
...
It's not necessary to specify "-u root" when "become: true". I haven't set a MySQL password for root.

inno setup giving error code 2 with mysql setup

I am executing this line in inno setup, but i am getting and exit code 2
;Setting root password default root (blank). ex : mypass4u#
Filename: "{app}\mysql\bin\mysqladmin.exe"; \
Parameters: "-u root -e ""update mysql.user set password=PASSWORD('mypass4u#') where user='root';"""; \
StatusMsg: "Setting password root"; \
Flags: runhidden;
I get the following message in the debug window
[11:56:54.387] -- Run entry -- [11:56:54.392] Run as: Current user
[11:56:54.396] Type: Exec
[11:56:54.400] Filename: C:\Program Files (x86)\Company\Myapp\mysql\bin\mysqladmin.exe
[11:56:54.405] Parameters: -u root -e "update mysql.user set
password=PASSWORD('mypass4u#') where user='root';"
[11:56:54.758] Process exit code: 2
What could be causing this error
I assume you wanted to use mysql.exe, not mysqladmin.exe.

How to extract file name from shell command from a ansible play

I am taking a db dump as follows:
- name: create backup of the EMS database
  shell: " mysqldump --single-transaction --triggers --routines --events --hex-blob --complete-insert -h {{groups.db_name[0]}} -u {{ db_user }} -p{{ db_password }} {{ db_name }} > {{ vars.inventory_dir }}/../{{ db_name}}_backup-{{ ansible_date_time.iso8601 }}.sql"
  register: db_backup
now if you notice the shell command at the end i dump the mysql with a date-time appended as {{ db_name}}_backup-{{ ansible_date_time.iso8601 }}.sql
How can i use only this dynamic part in my next play ?
I suggest you first store the filename as a fact:
- set_fact:
mysql_dump_file: "{{ db_name }}_backup-{{ ansible_date_time.iso8601 }}.sql"
Then in the dump task and all following tasks where you need the filename you use that fact: {{ mysql_dump_file }}
- name: create backup of the EMS database
shell: "mysqldump --single-transaction --triggers --routines --events --hex-blob --complete-insert -h {{ groups.db_name[0] }} -u {{ db_user }} -p{{ db_password }} {{ db_name }} > {{ vars.inventory_dir }}/../{{ mysql_dump_file }}"
register: db_backup