Skip to content

Commit 87a04c5

Browse files
committed
FixedAttempt to create database
1 parent dda0e53 commit 87a04c5

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

ansible/db-setup.yml

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,40 @@
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

0 commit comments

Comments
 (0)