Files
michaelschiemer/deployment/ansible/playbooks/test-redis-connection-direct.yml

122 lines
4.2 KiB
YAML

---
- name: Test Redis Connection Directly with php-redis Extension
hosts: production
gather_facts: yes
become: no
tasks:
- name: Check if php-redis extension is loaded
shell: |
docker exec app php -r "
if (extension_loaded('redis')) {
echo 'php-redis extension: LOADED' . PHP_EOL;
echo 'Redis class available: ' . (class_exists('Redis') ? 'YES' : 'NO') . PHP_EOL;
} else {
echo 'php-redis extension: NOT LOADED' . PHP_EOL;
}
"
args:
executable: /bin/bash
register: extension_check
ignore_errors: yes
failed_when: false
- name: Display extension check
debug:
msg: "{{ extension_check.stdout_lines }}"
- name: Test Redis connection with actual php-redis
shell: |
docker exec app php -r "
// Get environment variables
\$redis_host = getenv('REDIS_HOST') ?: 'redis';
\$redis_port = (int)(getenv('REDIS_PORT') ?: 6379);
\$redis_password = getenv('REDIS_PASSWORD');
echo '=== Redis Connection Test ===' . PHP_EOL;
echo 'REDIS_HOST: ' . \$redis_host . PHP_EOL;
echo 'REDIS_PORT: ' . \$redis_port . PHP_EOL;
echo 'REDIS_PASSWORD: ' . (\$redis_password ? 'SET (length: ' . strlen(\$redis_password) . ')' : 'NOT SET') . PHP_EOL;
echo PHP_EOL;
if (!extension_loaded('redis')) {
echo 'ERROR: php-redis extension is not loaded!' . PHP_EOL;
exit(1);
}
if (!class_exists('Redis')) {
echo 'ERROR: Redis class is not available!' . PHP_EOL;
exit(1);
}
try {
\$redis = new Redis();
echo 'Created Redis instance' . PHP_EOL;
// Connect
\$success = \$redis->connect(\$redis_host, \$redis_port, 2.0);
if (!\$success) {
echo 'ERROR: Failed to connect to Redis server' . PHP_EOL;
exit(1);
}
echo 'Connected to Redis server' . PHP_EOL;
// Authenticate if password is provided
if (\$redis_password) {
\$auth_result = \$redis->auth(\$redis_password);
if (!\$auth_result) {
echo 'ERROR: Redis authentication failed' . PHP_EOL;
echo 'Password used: ' . substr(\$redis_password, 0, 5) . '...' . PHP_EOL;
exit(1);
}
echo 'Authenticated with Redis' . PHP_EOL;
}
// Test PING
\$ping_result = \$redis->ping();
echo 'Redis PING: ' . \$ping_result . PHP_EOL;
// Test SET/GET
\$test_key = 'test_connection_' . time();
\$test_value = 'test_value';
\$set_result = \$redis->set(\$test_key, \$test_value);
echo 'SET test: ' . (\$set_result ? 'OK' : 'FAILED') . PHP_EOL;
\$get_result = \$redis->get(\$test_key);
echo 'GET test: ' . (\$get_result === \$test_value ? 'OK' : 'FAILED') . PHP_EOL;
// Cleanup
\$redis->del(\$test_key);
\$redis->close();
echo PHP_EOL . '? All tests passed!' . PHP_EOL;
} catch (Exception \$e) {
echo 'ERROR: ' . \$e->getMessage() . PHP_EOL;
echo 'Exception type: ' . get_class(\$e) . PHP_EOL;
exit(1);
}
"
args:
executable: /bin/bash
register: redis_test
ignore_errors: yes
failed_when: false
- name: Display Redis connection test results
debug:
msg: "{{ redis_test.stdout_lines }}"
- name: Check for Redis errors in application logs
shell: |
cd ~/deployment/stacks/application
docker compose logs app --tail=100 2>&1 | grep -i -E "(redis|connection|error)" | tail -20 || echo "No Redis errors found in logs"
args:
executable: /bin/bash
register: error_logs
ignore_errors: yes
failed_when: false
- name: Display error logs
debug:
msg: "{{ error_logs.stdout_lines }}"