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
Related
I'm trying to use ansible and docker to take a backup of my MySQL database. This is my ansible-task:
docker_container:
name: "{{ mysql_dump_container_name }}"
image: mysql:8.0.20
env:
MYSQL_ROOT_PASSWORD: "{{ mysql_password }}"
MYSQL_DATABASE: "{{ mysql_database }}"
ports:
- "3307:3307"
volumes:
- "{{ mysql_backup_dir }}:/backup"
entrypoint: "mysqldump -u {{ mysql_username }} -p{{ mysql_password }} --host {{ mysql_host }} {{ mysql_database }} > 1.sql "
detach: yes
restart_policy: "unless-stopped"
become: yes
I am getting the following error:
mysqldump: Couldn't find table: ">"
Entrypoint message is:
"entrypoint": [
"mysqldump -u root -proot --host wl.cs.net TEST > 1.sql"
],
What am I doing wrong?
Actually, you don't need to overwrite the entrypoint as the one that is already in the container will run your command. You just need to make sure that the > ends up at the right place. I do that by quoting the command.
You can do this:
docker_container:
name: "{{ mysql_dump_container_name }}"
image: "mysql:8.0.20"
volumes:
- "{{ mysql_backup_dir }}:/backup"
command: "/bin/sh -c 'mysqldump -u {{ mysql_username }} -p{{ mysql_password }} --host {{ mysql_host }} {{ mysql_database }} > /backup/1.sql'"
detach: false
cleanup: true
become: yes
Additional notes:
You do not need any port as the container is not listening for anything.
You don't need the env, as you aren't creating a database.
You probably want to write the dump to a file in /backup.
You probably don't want restart_policy: "unless-stopped" as it doesn't make sense to restart a failed backup, as it will probably fail again. (You need to find out why it failed, first)
You will probably want detach: false, so ansible will wait for the container to finish and then show failed if the backup failed.
You will probably want cleanup: true, so you don't need to clean up containers manually.
Links:
Documentation of the docker_container module
Redirecting command output in docker
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.
after running the following ansible script, root login on mysql server (root#localhost) pass login without password promp. (with cli commands have no problems)
- name: "Set the root password"
mysql_user:
login_user: root
login_password: ''
name: root
password: "{{ mysql_root_password }}"
priv: "*.*:ALL,GRANT"
check_implicit_admin: yes
host: localhost
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: "Remove all anonymous user accounts"
mysql_user:
name: ''
host_all: yes
state: absent
login_user: root
login_password: "{{ mysql_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
#mysql_secure_installation
- name: "Remove MySQL test database"
mysql_db:
name: test
state: absent
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: "Creates db"
mysql_db:
name: "{{ db_name }}"
state: present
login_user: root
login_password: "{{ mysql_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
For now I'm using this workaround and It's working correctly.
- name: "Set database password"
command: mysql --user=root --password={{ mysql_root_password }} --execute="SET PASSWORD FOR 'root'#'localhost' = PASSWORD('{{ mysql_root_password }}')";
- name: "Grant all to root"
command: mysql --user=root --password={{ mysql_root_password }} mysql --execute="GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost';";
- name: "Disable socket plugin"
command: mysql --user=root --password={{ mysql_root_password }} mysql --execute="update user set Plugin='' where User='root';";
- name: "Flush privileges"
command: mysql --user=root --password={{ mysql_root_password }} mysql --execute="flush privileges";
It looks it doesn't like unix_socket_plugin, but without, it doesn't connect even if I specify mysql-client password in .my.cnf
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.
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