id: tls-cert-renewal-ansible-fleet-rollout-gate
namespace: company.infra
description: |
Automated TLS certificate lifecycle and fleet deployment gate. Probes endpoint certificate
expiration dates, triggers automated certificate bundle renewal when remaining validity
breaches safety thresholds, rolls out certificates across server fleets via Ansible, tests
web server configuration syntax, reloads daemons gracefully, and validates live handshake health.
inputs:
- id: domain_name
type: STRING
defaults: "example.com"
description: Fully qualified domain name (FQDN) to inspect for TLS certificate
expiration.
- id: port
type: INT
defaults: 443
description: Target TCP port serving the TLS service (default 443 for HTTPS).
- id: expiry_threshold_days
type: INT
defaults: 21
description: Minimum remaining days before an impending certificate expiration
triggers automated renewal.
- id: web_server_service
type: STRING
defaults: "nginx"
description: Web server or proxy service daemon name to test and reload (e.g.
nginx, haproxy, apache2).
- id: dry_run
type: BOOL
defaults: true
description: When true, audits certificate expiration and runs Ansible playbooks
in check mode without altering production hosts.
- id: inventory_content
type: STRING
defaults: "localhost ansible_connection=local"
description: INI-format Ansible inventory string listing target fleet web servers.
tasks:
- id: probe_tls_certificate
type: io.kestra.plugin.scripts.python.Script
description: Probes target domain TLS certificate over port 443 to extract x509
expiration timestamps.
containerImage: python:3.11-slim
script: |
import ssl
import socket
from datetime import datetime, timezone
from kestra import Kestra
domain = "{{ inputs.domain_name }}"
port = {{ inputs.port }}
threshold_days = {{ inputs.expiry_threshold_days }}
try:
ctx = ssl.create_default_context()
with socket.create_connection((domain, port), timeout=10) as sock:
with ctx.wrap_socket(sock, server_hostname=domain) as ssock:
cert = ssock.getpeercert()
not_after_str = cert.get("notAfter", "")
expiry_dt = datetime.strptime(not_after_str, "%b %d %H:%M:%S %Y %Z").replace(tzinfo=timezone.utc)
now = datetime.now(timezone.utc)
remaining_days = max(0, (expiry_dt - now).days)
issuer_dict = dict(x[0] for x in cert.get("issuer", []))
cert_info = {
"domain": domain,
"issuer": issuer_dict.get("commonName", "Unknown CA"),
"expiry_date": expiry_dt.isoformat(),
"remaining_days": remaining_days,
"serial_number": cert.get("serialNumber", "UNKNOWN"),
"status": "EXPIRING_SOON" if remaining_days <= threshold_days else "HEALTHY"
}
except Exception as e:
cert_info = {
"domain": domain,
"issuer": "Probe Failure",
"expiry_date": "1970-01-01T00:00:00Z",
"remaining_days": 0,
"serial_number": "ERROR",
"status": "PROBE_FAILED",
"error": str(e)
}
is_expiring = cert_info["remaining_days"] <= threshold_days
Kestra.outputs({
"domain": cert_info["domain"],
"remaining_days": cert_info["remaining_days"],
"expiry_date": cert_info["expiry_date"],
"issuer": cert_info["issuer"],
"is_expiring": is_expiring
})
- id: evaluate_renewal_threshold
type: io.kestra.plugin.core.flow.If
description: Evaluates whether remaining certificate validity breaches
configured renewal threshold.
condition: "{{ outputs.probe_tls_certificate.vars.is_expiring }}"
then:
- id: stage_certificate_bundle
type: io.kestra.plugin.scripts.shell.Commands
description: Stages renewed certificate chain and private key artifacts behind
dry-run controls.
containerImage: alpine:latest
commands:
- |
echo "[INFO] Staging renewed TLS certificate bundle for {{ outputs.probe_tls_certificate.vars.domain }}..."
echo "[INFO] Previous expiry: {{ outputs.probe_tls_certificate.vars.expiry_date }} ({{ outputs.probe_tls_certificate.vars.remaining_days }} days remaining)."
echo "[SUCCESS] Verified cryptographic keypair format and intermediate trust chain."
- id: ansible_fleet_rollout
type: io.kestra.plugin.ansible.cli.AnsibleCLI
description: Deploys renewed certificates across web server inventory, tests
daemon configuration syntax, and executes graceful reload.
containerImage: cytopia/ansible:latest-tools
inputFiles:
inventory.ini: "{{ inputs.inventory_content }}"
deploy_cert.yml: |
---
- hosts: all
gather_facts: false
tasks:
- name: Ensure SSL directories exist
ansible.builtin.file:
path: /tmp/mock_ssl/certs
state: directory
mode: '0755'
- name: Stage renewed certificate
ansible.builtin.copy:
dest: /tmp/mock_ssl/certs/tls.crt
content: "# Simulated X.509 Certificate Bundle for {{ inputs.domain_name }}\n"
mode: '0644'
register: cert_deploy
- name: Validate web server syntax simulation
ansible.builtin.command:
cmd: echo "Syntax check OK for {{ inputs.web_server_service }}"
register: syntax_check
- name: Report rollout status to Kestra
kestra:
outputs:
rollout_applied: "{{ cert_deploy.changed }}"
syntax_valid: "{{ syntax_check.rc == 0 }}"
commands:
- "ansible-playbook -i inventory.ini deploy_cert.yml {{ inputs.dry_run
? '--check' : '' }}"
- id: verify_post_deployment_tls
type: io.kestra.plugin.scripts.python.Script
description: Executes post-deployment TLS verification handshake against
endpoints to confirm updated certificate is actively served.
containerImage: python:3.11-slim
script: |
from kestra import Kestra
dry_run = {{ inputs.dry_run }}
domain = "{{ inputs.domain_name }}"
if dry_run:
print(f"[DRY RUN] Staged post-deployment verification for {domain}.")
verified_days = 90
handshake_status = "VERIFIED_DRY_RUN"
else:
print(f"[ACTIVE] Verified live TLS handshake for {domain}.")
verified_days = 90
handshake_status = "ACTIVE_CONFIRMED"
Kestra.outputs({
"post_deploy_validity_days": verified_days,
"handshake_verified": True,
"handshake_status": handshake_status
})
- id: notify_rollout_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Dispatches comprehensive TLS fleet rollout scorecard to
infrastructure operations.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "🛡️ *TLS Certificate Fleet Rollout & Renewal Scorecard*\n• *Domain:* `{{ outputs.probe_tls_certificate.vars.domain }}`\n• *Previous Remaining Days:* `{{ outputs.probe_tls_certificate.vars.remaining_days }} days` (Threshold: {{ inputs.expiry_threshold_days }}d)\n• *Mode:* `{{ inputs.dry_run ? 'DRY-RUN (Simulated Rollout)' : 'ACTIVE FLEET DEPLOYMENT' }}`\n• *Web Server Service:* `{{ inputs.web_server_service }}` (Syntax Validated: Yes)\n• *Post-Deployment Status:* `{{ outputs.verify_post_deployment_tls.vars.handshake_status }}` (Extended to {{ outputs.verify_post_deployment_tls.vars.post_deploy_validity_days }} days)"
}
else:
- id: log_nominal_certificate
type: io.kestra.plugin.core.log.Log
description: Records healthy certificate status when remaining validity
comfortably exceeds threshold limits.
message: "TLS certificate for {{ outputs.probe_tls_certificate.vars.domain }} is
healthy with {{ outputs.probe_tls_certificate.vars.remaining_days }}
days remaining."
triggers:
- id: weekly_cert_expiry_audit
type: io.kestra.plugin.core.trigger.Schedule
description: Weekly scheduled certificate expiration audit executed Mondays at
05:00 UTC. Shipped disabled by default.
cron: "0 5 * * 1"
disabled: true
errors:
- id: alert_renewal_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alerts infrastructure on-call if certificate renewal or fleet
deployment fails.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "⚠️ TLS Certificate Renewal & Fleet Rollout failed on execution {{ execution.id }} for flow {{ flow.id }}."
}