--- 
- name: "Check client ca certificate"
  register: ca_cert
  stat: "path={{ x509_cacert_file }}"

- name: "Check certificate and key"
  shell: (openssl x509 -noout -modulus -in {{ x509_cert_file }}  | openssl md5 ; openssl rsa -noout -modulus -in {{ x509_key_file }} | openssl md5) | uniq | wc -l
  register: certcheck

- name: "Check certificate"
  register: cert
  stat: "path={{ x509_cert_file }}"

- name: "Check key"
  register: key
  stat: "path={{ x509_key_file }}"
  become: true

- name: "Default: we don't need a new certificate"
  set_fact: needcert=False

- name: "Set need cert if key is missing"
  set_fact: needcert=True
  when: key.stat.exists == false

- name: "set needcert if cert is missing"
  set_fact: needcert=True
  when: cert.stat.exists == false

- name: "set needcert if cert doesn't match key"
  set_fact: needcert=True
  when: certcheck.stdout == '2'


- name: "Creating Keypair"
  shell: "echo noop when using easy-rsa"
  when: needcert

- name: "Creating CSR"
  shell: " cd /etc/easy-rsa/2.0; source ./vars; export EASY_RSA=\"${EASY_RSA:-.}\"; \"$EASY_RSA\"/pkitool --csr {{ x509_csr_args }} {{ common_name }}"
  when: needcert
  become: true

- name: "Copy CSR to ansible host"
  fetch: "src=/etc/easy-rsa/2.0/keys/{{ common_name }}.csr dest=/tmp/{{ common_name }}/ fail_on_missing=yes validate_md5=yes flat=yes"
  become: true
  when: needcert

- name: "Copy CSR to CA"
  delegate_to: "{{ x509_ca_server }}"
  copy: "src=/tmp/{{ ansible_fqdn }}/{{ common_name }}.csr dest=/etc/easy-rsa/2.0/keys/{{ common_name }}.csr force=yes"
  when: needcert
  become: true

- name: "Sign Certificate"
  delegate_to: "{{ x509_ca_server }}"
  shell:    "source ./vars; export EASY_RSA=\"${EASY_RSA:-.}\" ;\"$EASY_RSA\"/pkitool --sign {{ common_name }}"
  args:
    chdir: "/etc/easy-rsa/2.0"
  become: true
  when: needcert

- name: "Copy the Certificate to ansible host"
  delegate_to: "{{ x509_ca_server }}"
  fetch: "src=/etc/easy-rsa/2.0/keys/{{ common_name }}.crt dest=/tmp/{{ common_name }}/ fail_on_missing=yes validate_md5=yes flat=yes"
  become: true
  when: needcert

- name: "Copy the CA Certificate to the ansible host"
  delegate_to: "{{ x509_ca_server }}"
  fetch: "src=/etc/easy-rsa/2.0/keys/ca.crt dest=/tmp/ca.crt fail_on_missing=yes validate_md5=yes flat=yes"
  become: true
  when: "ca_cert.stat.exists == false"

- name: "Copy the certificate to the node"
  copy: "src=/tmp/{{ common_name }}/{{ common_name }}.crt dest={{ x509_cert_file }} force=yes"
  become: true
  when: needcert

- name: "Copy the CA certificate to the node"
  copy: "src=/tmp/ca.crt dest={{ x509_cacert_file }}"
  become: true
  when: "ca_cert.stat.exists == false"

- name: "Copy the key to the correct location"
  shell: "mkdir -p `dirname {{ x509_key_file }}` ; chmod 700 `dirname {{ x509_key_file }}` ; cp /etc/easy-rsa/2.0/keys/{{ common_name }}.key {{ x509_key_file }}"
  become: true
  when: needcert
