Skip to content

Instantly share code, notes, and snippets.

@JonTheNiceGuy
Created December 17, 2019 10:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonTheNiceGuy/1e29771b544f99a86adda1c1d03919ae to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/1e29771b544f99a86adda1c1d03919ae to your computer and use it in GitHub Desktop.
Create CA and Client Certificates on a protected CA Server, and transfer to them to your nodes, using Ansible

Create CA and Client Certificates on a protected CA Server, and transfer to them to your nodes, using Ansible

This is based on https://kapuablog.wordpress.com/2019/11/26/ansible-reading-a-remote-yaml-file/

It has been used in $my_service to generate client certificates on a protected Certificate Authority (CA) server (accessible only to my Ansible Tower/AWX server) and is then distributed to the client nodes.

- hosts: all
tasks:
- name: Create Client Certificate
become: true
delegate_to: "{{ ca_server }}"
shell:
cmd: "/opt/my_service/create_cert -name {{ inventory_hostname }}"
chdir: /etc/my_service
creates: "/etc/my_service/{{ inventory_hostname }}.crt"
- name: Read CA.crt, node.crt and node.key
# Based on https://kapuablog.wordpress.com/2019/11/26/ansible-reading-a-remote-yaml-file/
become: true
delegate_to: "{{ ca_server }}"
command: "cat '/etc/my_service/{{ item }}'"
register: keymaterials
loop:
- ca.crt
- "{{ inventory_hostname }}.crt"
- "{{ inventory_hostname }}.key"
- name: Transfer certificates to target
become: true
copy:
content: "{{ item.stdout }}"
dest: "/etc/my_service/{{ item.item }}"
owner: root
group: root
mode: 0700
loop: "{{ keymaterials.results | default([]) }}"
loop_control:
label: "{{ item.item }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment