File tree Expand file tree Collapse file tree 1 file changed +23
-13
lines changed
Expand file tree Collapse file tree 1 file changed +23
-13
lines changed Original file line number Diff line number Diff line change 4444 group : root
4545 mode : " 0644"
4646
47- - name : Create MySQL database
47+
48+ - name : Handle MySQL database creation
4849 block :
4950 - name : Attempt to create database
5051 command : >
5152 mysql -h "{{ db_host }}" -u "{{ db_user }}" -p"{{ db_pass }}"
52- -e "CREATE DATABASE IF NOT EXISTS {{ db_name }};"
53- args :
54- warn : false # Suprime warnings sobre el módulo command
53+ -e "CREATE DATABASE IF NOT EXISTS {{ db_name }}; SELECT 'success' as status;"
5554 register : create_db
5655 no_log : true
5756 ignore_errors : yes
58- changed_when :
59- - " 'already exists' not in create_db.stderr"
60- - " 'database exists' not in create_db.stderr" # Algunas versiones usan este mensaje
57+ changed_when : false # We'll handle changes manually
58+
59+ - name : Check if database exists
60+ command : >
61+ mysql -h "{{ db_host }}" -u "{{ db_user }}" -p"{{ db_pass }}"
62+ -e "SHOW DATABASES LIKE '{{ db_name }}';"
63+ register : db_check
64+ no_log : true
65+ ignore_errors : yes
66+ changed_when : false
6167
6268 - name : Validate database creation
6369 fail :
64- msg : " Failed to create database. MySQL error: {{ create_db.stderr | default('Unknown error') }}"
65- when :
66- - create_db is failed
67- - " 'already exists' not in create_db.stderr"
68- - " 'database exists' not in create_db.stderr"
69- no_log : false # Muestra el error real para diagnóstico
70+ msg : " Failed to create database '{{ db_name }}'. Connection or permissions issue."
71+ when : >
72+ "'success' not in create_db.stdout" and
73+ db_check.stdout.find(db_name) == -1
74+ no_log : false
7075
76+ rescue :
77+ - name : Display database creation error
78+ debug :
79+ msg : " Database creation error: {{ create_db.stderr | default('Unknown error') }}"
80+ no_log : false
7181
7282 - name : Load MySQL initialization script (run once)
7383 run_once : true
You can’t perform that action at this time.
0 commit comments