-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vault agent role for ansible #10
Changes from 4 commits
d0a9a9b
03641fb
d39ed1b
d97e736
0b2102b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[Unit] | ||
Description=Vault Agent - retrieve Let's Encrypt certificates from Vault | ||
After=network.target | ||
|
||
[Service] | ||
Type=notify | ||
WorkingDirectory=/root/vault_agent_certificat | ||
ExecStart=/usr/bin/vault agent -config=agent-config.hcl | ||
Restart=always | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- name: Restart vault-agent-certificates | ||
ansible.builtin.systemd: | ||
name: vault-agent-certificates | ||
state: restarted |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
- name: Download Vault Hashicorp gpg key | ||
ansible.builtin.get_url: | ||
url: https://apt.releases.hashicorp.com/gpg | ||
dest: /usr/share/keyrings/hashicorp-archive-keyring.asc | ||
mode: "0644" | ||
owner: root | ||
checksum: sha256:cafb01beac341bf2a9ba89793e6dd2468110291adfbb6c62ed11a0cde6c09029 | ||
|
||
- name: Add Vault repository | ||
ansible.builtin.apt_repository: | ||
repo: "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.asc] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main" | ||
state: present | ||
|
||
- name: Intall Vault | ||
ansible.builtin.apt: | ||
update_cache: true | ||
name: | ||
- vault | ||
Comment on lines
+22
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe not for this PR as our needs are simple and Vault-Agent should be mostly compatible with any version of Vault server (https://developer.hashicorp.com/vault/docs/agent-and-proxy/agent/versions), but at some point we might want to pin a specific version of packages, especially Vault since it's a critical part of our infrastructure and we should check changelogs and ensure backups before upgrades. The Ansible docs hint at Explanation: Ansible - Vault pinning
Package: vault
Pin: version 1.18.2-*
Pin-Priority: 1000 |
||
|
||
- name: Retrieve role_id and secret_id from Vault | ||
community.hashi_vault.vault_kv2_get: | ||
path: certificat-web-id | ||
register: certificat_secrets | ||
run_once: true | ||
delegate_to: localhost | ||
become: false | ||
|
||
- name: Create vault agent workdir | ||
ansible.builtin.file: | ||
path: /root/vault_agent_certificat | ||
state: directory | ||
mode: '0755' | ||
owner: root | ||
group: root | ||
|
||
- name: Create role_id file | ||
vars: | ||
content: "{{ certificat_secrets.secret.role_id }}" | ||
ansible.builtin.template: | ||
src: content.j2 | ||
dest: /root/vault_agent_certificat/role_id | ||
mode: '0600' | ||
owner: root | ||
group: root | ||
|
||
- name: Create secret_id file | ||
vars: | ||
content: "{{ certificat_secrets.secret.secret_id }}" | ||
ansible.builtin.template: | ||
src: content.j2 | ||
dest: /root/vault_agent_certificat/secret_id | ||
mode: '0600' | ||
owner: root | ||
group: root | ||
|
||
- name: Copy agent-config.hcl | ||
ansible.builtin.template: | ||
src: agent-config.hcl.j2 | ||
dest: /root/vault_agent_certificat/agent-config.hcl | ||
mode: '0644' | ||
owner: root | ||
group: root | ||
notify: Restart vault-agent-certificates | ||
|
||
- name: Copy retrieving_cert.tmpl | ||
ansible.builtin.template: | ||
src: retrieving_cert.tmpl.j2 | ||
dest: /root/vault_agent_certificat/retrieving_cert.tmpl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be a good idea to change the template variable delimiters for this task so we don't have to escape all the |
||
mode: '0644' | ||
owner: root | ||
group: root | ||
variable_start_string: '<<' | ||
variable_end_string: '>>' | ||
notify: Restart vault-agent-certificates | ||
|
||
- name: Copy vault-agent-certificates.service | ||
ansible.builtin.copy: | ||
src: vault-agent-certificates.service | ||
dest: /etc/systemd/system/vault-agent-certificates.service | ||
mode: '0644' | ||
owner: root | ||
group: root | ||
|
||
- name: Create directory for certificates | ||
ansible.builtin.file: | ||
state: directory | ||
dest: "{{ vault_agent_certificate_directory }}" | ||
mode: '0755' | ||
owner: root | ||
group: root | ||
|
||
- name: Start vault-agent-certificates service | ||
ansible.builtin.systemd: | ||
name: vault-agent-certificates | ||
state: started | ||
enabled: true | ||
daemon_reload: true |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||||
vault { | ||||||||
address = "https://vault.rezoleo.fr" | ||||||||
retry { | ||||||||
num_retries = 5 | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
auto_auth { | ||||||||
method { | ||||||||
type = "approle" | ||||||||
|
||||||||
config = { | ||||||||
// create a role in vault with a policy able to "read" the secret | ||||||||
role_id_file_path = "role_id" // to change based on the path of the role_id file | ||||||||
secret_id_file_path = "secret_id" // to change based on the path of the secret_id file | ||||||||
remove_secret_id_file_after_reading = false | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
sinks { | ||||||||
sink { | ||||||||
type = "file" | ||||||||
|
||||||||
config = { | ||||||||
path = "sink-token" | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
template { | ||||||||
source = "retrieving_cert.tmpl" | ||||||||
destination = "{{ vault_agent_certificate_directory }}/template_output" | ||||||||
perms = "0600" | ||||||||
|
||||||||
exec { | ||||||||
command = {{service_reload_command | tojson}} {# to reload the service after retrieving the certificate #} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also I found out that since we did this last year, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I am not mistaken the parameter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mondays are hard... You are right, sorry 😅 |
||||||||
|
||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{content}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{{with secret "secret/certificat-web"}} | ||
{{ index .Data.data "privkey.pem" | writeToFile "<<vault_agent_certificate_directory>>/privkey.pem" "" "" "0400" }} | ||
{{ index .Data.data "chain.pem" | writeToFile "<<vault_agent_certificate_directory>>/chain.pem" "" "" "0400" }} | ||
{{ index .Data.data "cert.pem" | writeToFile "<<vault_agent_certificate_directory>>/cert.pem" "" "" "0400" }} | ||
{{ index .Data.data "fullchain.pem" | writeToFile "<<vault_agent_certificate_directory>>/fullchain.pem" "" "" "0400" }} | ||
{{ index .Data.data "privkey.pem" }} | ||
{{ index .Data.data "chain.pem" }} | ||
{{ index .Data.data "cert.pem" }} | ||
{{ index .Data.data "fullchain.pem" }} | ||
{{end}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be consistent ^^
And when you have a path that is repeated in many places (here and in tasks.yml), you can create a default variable (in
roles/vault_agent/defaults/main.yml
) and reuse it everywhere, e.g.