title: Ansible: Create a Self-Signed SSL Certificate and Key
body: <p>I want to create a self signed certificate to use it with stunnel, in order to securely tunnel my redis traffic between the redis server and client. I'm using this command to generate the certificate and it works fine.</p>
  
  <pre><code>openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/stunnel/redis-server.key -out /etc/stunnel/redis-server.crt
  </code></pre>
  
  <p>Since I'm using Ansible for provisioning, I would like to know how I can convert this into a more Ansible way of doing, using a module. There actually is a module called the <a href="https://docs.ansible.com/ansible/latest/modules/openssl_certificate_module.html" rel="noreferrer">openssl_certificate</a> Ansible module and it states <em>"This module allows one to (re)generate OpenSSL certificates."</em>. I tried to use the module to generate the certificate, but I couldn't get it to work.</p>
  
  <pre><code>- name: Generate a Self Signed OpenSSL certificate
    openssl_certificate:
      path: /etc/stunnel/redis-server.crt
      privatekey_path: /etc/stunnel/redis-server.key
      csr_path: /etc/stunnel/redis-server.csr
      provider: selfsigned
  </code></pre>
  
  <p>From a look at the documentation, I can't specify the following arguments <code>-x509 -nodes -days 3650 -newkey rsa:2048</code>. Of course, I could also split the key and cert generation, but that still wouldn't allow me to use the Ansible module, correct?</p>
  
  <p>Example given: </p>
  
  <pre><code>openssl genrsa -out /etc/stunnel/key.pem 4096
  
  openssl req -new -x509 -key /etc/stunnel/key.pem -out /etc/stunnel/cert.pem -days 1826
  </code></pre>
  
  <p>I would like to know the following things:</p>
  
  <ul>
  <li>a) How can I get the same result from the original command, but using an Ansible module?</li>
  <li>b) Is there a better way to manage self signed certificates using Ansible?</li>
  </ul>