Setting Up a Self-Signed Certificate in Ansible

by Noah Rotheray · Reviewed by Leo Almeida · Published · Updated · 3 minute read

Create a self-signed TLS certificate with Ansible: a complete community.crypto playbook (openssl_privatekey, openssl_csr, x509_certificate) and when a self-signed certificate is the wrong choice.

To create a self-signed certificate with Ansible, use three modules from the community.crypto collection: openssl_privatekey generates the key, openssl_csr builds the signing request, and x509_certificate with provider: selfsigned signs it. A self-signed certificate encrypts traffic but isn't trusted by browsers or API clients, so use it for development, test and internal lab hosts only.

This post shows a complete playbook and explains when a self-signed certificate is the wrong choice.

Self-Signed vs. Certificate Authority (CA)

Before using a self-signed certificate, you should know the difference between installing a certificate from a CA and using self-signed certificates. Self-signed certificates are untrusted and should only be used in development environments. They are a workaround for SSL/TLS validation while you develop an application.

In a production environment, you generate private keys to create a CSR (certified signing request) signed and validated by a CA that then tells users and applications that the certificate should be trusted. If you use a self-signed certificate in production, users will receive warnings not to trust the host. Self-signed certificates can be used in phishing attacks, so modern browsers warn users not to trust a host without a valid CA-signed certificate.

In a development environment, a self-signed certificate might be necessary, but know that they should never be used in production.

Creating a Self-Signed Certificate

The first step before certificate creation is to ensure Ansible is installed. Run the following command to get the Ansible version:

$ ansible --version

The modules live in the community.crypto collection, which the full ansible package already includes. With ansible-core only, install it, and make sure the managed host has the Python cryptography library (for example the python3-cryptography package):

$ ansible-galaxy collection install community.crypto

Creating a self-signed certificate takes three steps:

  • Create a private key with community.crypto.openssl_privatekey
  • Create a CSR with community.crypto.openssl_csr
  • Sign the CSR with its own key using community.crypto.x509_certificate and provider: selfsigned

The private key never leaves the server. In the TLS handshake, the server uses it to prove it owns the certificate, by signing the handshake, while the session keys come from an ephemeral (EC)DHE key exchange. That's why the key file needs tight permissions.

Here is the whole playbook:

- name: Self-signed certificate for a dev host
  hosts: dev
  become: true
  tasks:
    - name: Private key
      community.crypto.openssl_privatekey:
        path: /etc/ssl/private/dev.example.com.key
        type: ECC
        curve: secp256r1
        mode: "0600"

    - name: CSR
      community.crypto.openssl_csr:
        path: /etc/ssl/private/dev.example.com.csr
        privatekey_path: /etc/ssl/private/dev.example.com.key
        common_name: dev.example.com
        subject_alt_name:
          - DNS:dev.example.com

    - name: Self-signed certificate, valid 90 days
      community.crypto.x509_certificate:
        path: /etc/ssl/certs/dev.example.com.crt
        privatekey_path: /etc/ssl/private/dev.example.com.key
        csr_path: /etc/ssl/private/dev.example.com.csr
        provider: selfsigned
        selfsigned_not_after: "+90d"

The key is ECDSA P-256, which is smaller and faster than RSA and supported by every current client. For RSA, use type: RSA and size: 2048 or more (the module defaults to 4096). Clients match the hostname against subject_alt_name, not the common name, so always set it.

In a production environment you would send the CSR to a CA instead of signing it yourself, or skip the CSR entirely and let an ACME client such as Certbot request a certificate from Let's Encrypt.

After this step, you can use the CRT file to set up encrypted connections on your server. Every certificate has an expiration date: the playbook above sets 90 days, and public certificates are capped at 200 days since March 2026. You must renew the certificate before it expires. Should you forget to renew the certificate, you will receive errors in production or in your development environment. Forgetting to renew an expired certificate can crash services and cause critical downtime for applications running in production or development. Common browsers such as Chrome warn users when server certificates have expired. You can avoid these errors by monitoring your SSL/TLS certificates expiration dates and renewing them before they expire.