Files
michaelschiemer/deployment/ansible/playbooks/check-redis-connection.yml

194 lines
6.7 KiB
YAML

---
- name: Check Redis Connection and Environment Variables in PHP Container
hosts: production
gather_facts: yes
become: no
tasks:
- name: Check if application stack is running
shell: |
cd ~/deployment/stacks/application
docker compose ps
args:
executable: /bin/bash
register: stack_status
ignore_errors: yes
failed_when: false
- name: Display stack status
debug:
msg: "{{ stack_status.stdout_lines }}"
- name: Check PHP container exists
shell: |
docker ps --filter "name=app"
args:
executable: /bin/bash
register: php_container
ignore_errors: yes
failed_when: false
- name: Display PHP container status
debug:
msg: "{{ php_container.stdout_lines }}"
- name: Check Environment Variables in PHP Container
shell: |
echo "=== Redis Environment Variables in PHP Container ==="
docker exec app env | grep -E "(REDIS_|CACHE_|SESSION_|QUEUE_)" || echo "Container not accessible or no Redis vars found"
echo ""
echo "=== All Environment Variables in PHP Container ==="
docker exec app env | sort | head -50 || echo "Container not accessible"
args:
executable: /bin/bash
register: env_vars
ignore_errors: yes
failed_when: false
- name: Display environment variables
debug:
msg: "{{ env_vars.stdout_lines }}"
- name: Test Redis Connection from PHP Container
shell: |
echo "=== Testing Redis Connection from PHP Container ==="
echo "Test 1: Check if Redis is reachable"
docker exec app php -r "
\$redis_host = getenv('REDIS_HOST') ?: 'redis';
\$redis_port = (int)(getenv('REDIS_PORT') ?: 6379);
\$redis_password = getenv('REDIS_PASSWORD');
echo \"REDIS_HOST: \" . \$redis_host . \"\\n\";
echo \"REDIS_PORT: \" . \$redis_port . \"\\n\";
echo \"REDIS_PASSWORD: \" . (\$redis_password ? 'SET (length: ' . strlen(\$redis_password) . ')' : 'NOT SET') . \"\\n\";
// Test TCP connection
\$socket = @fsockopen(\$redis_host, \$redis_port, \$errno, \$errstr, 2);
if (\$socket) {
echo \"TCP Connection: OK\\n\";
fclose(\$socket);
} else {
echo \"TCP Connection: FAILED (errno: \$errno, errstr: \$errstr)\\n\";
}
// Test with Predis if available
if (class_exists('Predis\\Client')) {
try {
\$client = new Predis\\Client([
'scheme' => 'tcp',
'host' => \$redis_host,
'port' => \$redis_port,
'password' => \$redis_password ?: null,
]);
\$client->connect();
echo \"Predis Connection: OK\\n\";
echo \"Redis PING: \" . \$client->ping() . \"\\n\";
\$client->disconnect();
} catch (Exception \$e) {
echo \"Predis Connection: FAILED - \" . \$e->getMessage() . \"\\n\";
}
} else {
echo \"Predis not available\\n\";
}
" || echo "Could not execute PHP test"
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 Redis Container Configuration
shell: |
echo "=== Redis Container Status ==="
docker ps --filter "name=redis"
echo ""
echo "=== Redis Container Environment ==="
docker exec redis env | grep -E "(REDIS_|REQUIREPASS)" || echo "No Redis env vars found"
echo ""
echo "=== Test Redis Password ==="
REDIS_PASSWORD=$(cd ~/deployment/stacks/application && grep REDIS_PASSWORD .env | cut -d '=' -f2 | tr -d ' ' || echo "")
if [ -n "$REDIS_PASSWORD" ]; then
PASSWORD_LEN=$(echo -n "$REDIS_PASSWORD" | wc -c)
echo "REDIS_PASSWORD from .env file: SET (length: $PASSWORD_LEN)"
docker exec redis redis-cli -a "$REDIS_PASSWORD" PING || echo "Redis password test failed"
else
echo "REDIS_PASSWORD from .env file: NOT SET"
docker exec redis redis-cli PING || echo "Redis connection test failed (no password)"
fi
args:
executable: /bin/bash
register: redis_config
ignore_errors: yes
failed_when: false
- name: Display Redis container configuration
debug:
msg: "{{ redis_config.stdout_lines }}"
- name: Check Docker Network Connectivity
shell: |
echo "=== Docker Network: app-internal ==="
docker network inspect app-internal 2>&1 | grep -E "(Name|Subnet|Containers)" | head -20 || echo "Network not found"
echo ""
echo "=== Testing Network Connectivity ==="
echo "From PHP container to Redis:"
docker exec app ping -c 2 redis 2>&1 || echo "Ping test failed"
echo ""
echo "From PHP container to Redis (port 6379):"
docker exec app nc -zv redis 6379 2>&1 || echo "Port test failed"
args:
executable: /bin/bash
register: network_test
ignore_errors: yes
failed_when: false
- name: Display network connectivity test
debug:
msg: "{{ network_test.stdout_lines }}"
- name: Check Application Logs for Redis Errors
shell: |
cd ~/deployment/stacks/application
echo "=== Application Logs (Last 50 lines, Redis-related) ==="
docker compose logs app --tail=50 2>&1 | grep -i redis || echo "No Redis-related logs found"
args:
executable: /bin/bash
register: app_logs
ignore_errors: yes
failed_when: false
- name: Display application logs
debug:
msg: "{{ app_logs.stdout_lines }}"
- name: Check .env file configuration
shell: |
cd ~/deployment/stacks/application
echo "=== .env file Redis Configuration ==="
if [ -f .env ]; then
grep -E "(REDIS_|CACHE_|SESSION_|QUEUE_)" .env | grep -v "^#" || echo "No Redis config found in .env"
else
echo ".env file not found"
fi
echo ""
echo "=== Checking for application.env file ==="
if [ -f application.env ]; then
echo "application.env exists"
grep -E "(REDIS_|CACHE_|SESSION_|QUEUE_)" application.env | grep -v "^#" || echo "No Redis config found in application.env"
else
echo "application.env file not found"
fi
args:
executable: /bin/bash
register: env_file_config
ignore_errors: yes
failed_when: false
- name: Display .env file configuration
debug:
msg: "{{ env_file_config.stdout_lines }}"