feat(vault): enable consul service registration automatically if consul is also enabled
All checks were successful
development / Check commit compliance (push) Successful in 11s
pull-requests-open / Check commit compliance (pull_request) Successful in 8s

This commit is contained in:
Bertrand Lanson 2024-05-07 19:23:48 +02:00
parent d170cdf3da
commit 83f9c9748e
7 changed files with 64 additions and 84 deletions

View File

@ -6,7 +6,7 @@
# enable_haproxy: "yes" # enable_haproxy: "yes"
# enable_vault: "yes" # enable_vault: "yes"
# enable_consul: "yes" # enable_consul: "yes"
# enable_nomad: "no" enable_nomad: "no"
# haproxy_version: "2.8" # haproxy_version: "2.8"
# nomad_version: "1.7.7" # nomad_version: "1.7.7"

View File

@ -174,12 +174,12 @@ vault_extra_listener_configuration: {}
# service registration # # service registration #
######################## ########################
vault_enable_service_registration: false vault_enable_service_registration: "{{ enable_consul | bool }}"
vault_service_registration_configuration: vault_service_registration_configuration:
consul: consul:
address: "127.0.0.1:8500" address: "127.0.0.1:{{ hostvars[groups['consul_servers'][0]].consul_api_port[hostvars[groups['consul_servers'][0]].consul_api_scheme] }}"
scheme: "http" scheme: "{{ hostvars[groups['consul_servers'][0]].consul_api_scheme }}"
token: "" token: "{{ _credentials.consul.tokens.vault.secret_id }}"
################# #################
# vault plugins # # vault plugins #

View File

@ -49,6 +49,15 @@ vault_plugin_extra_files_dir:
- src: "{{ sub_configuration_directories['vault_servers'] }}/plugin" - src: "{{ sub_configuration_directories['vault_servers'] }}/plugin"
dest: "{{ vault_plugin_directory }}" dest: "{{ vault_plugin_directory }}"
##############################
# vault service registration #
##############################
vault_service_registration_policy: |
service "vault" {
policy = "write"
}
######################## ########################
# vault role variables # # vault role variables #
######################## ########################

View File

@ -14,6 +14,7 @@ consul03
[consul_agents:children] [consul_agents:children]
haproxy_servers haproxy_servers
vault_servers
[nomad_servers] [nomad_servers]
nomad01 nomad01

View File

@ -29,28 +29,6 @@
register: _consul_init_secret register: _consul_init_secret
when: hashi_consul_configuration.acl.enabled when: hashi_consul_configuration.acl.enabled
# - name: "Write consul configuration to file" # noqa: run-once[task] no-handler
# ansible.builtin.blockinfile:
# marker: "## -- {root_token} ANSIBLE MANAGED BLOCK ##"
# block: "{{
# {
# 'root_token':{
# 'accessor_id': _consul_init_secret.result.AccessorID,
# 'secret_id': _consul_init_secret.result.SecretID
# }
# } | to_nice_yaml
# }}"
# path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
# mode: "0644"
# when: _consul_init_secret.result is defined
# run_once: true
# delegate_to: localhost
# - name: "Load consul cluster variables"
# ansible.builtin.include_vars:
# file: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
# name: _consul_cluster_config
- name: "Create consul agents token" - name: "Create consul agents token"
when: when:
- _consul_init_secret.changed - _consul_init_secret.changed
@ -72,22 +50,18 @@
register: _consul_agent_policy register: _consul_agent_policy
- name: "Create consul agents token" - name: "Create consul agents token"
vars: community.general.consul_token:
consul_token_body: token: "{{ _credentials.consul.root_token.secret_id }}"
Description: "Consul agents token" host: "{{ api_interface_address }}"
SecretID: "{{ _credentials.consul.tokens.agent }}" port: "{{ consul_api_port[consul_api_scheme] }}"
Policies: scheme: "{{ consul_api_scheme }}"
- ID: "{{ _consul_agent_policy.policy.ID }}" validate_certs: false
ansible.builtin.uri: accessor_id: "{{ _credentials.consul.tokens.agent.accessor_id }}"
url: "{{ consul_api_addr }}/v1/acl/token" secret_id: "{{ _credentials.consul.tokens.agent.secret_id }}"
method: PUT policies:
headers: - id: "{{ _consul_agent_policy.policy.ID }}"
X-Consul-Token: "{{ _credentials.consul.root_token.secret_id }}" state: present
body: "{{ consul_token_body | to_json }}"
status_code:
- 200
register: _consul_agent_token register: _consul_agent_token
changed_when: _consul_agent_token.status == 200
- name: "Restart consul service" - name: "Restart consul service"
ansible.builtin.service: ansible.builtin.service:
@ -95,44 +69,3 @@
state: restarted state: restarted
throttle: 1 throttle: 1
when: _consul_agent_token.changed when: _consul_agent_token.changed
# - name: "Write consul agents token to file" # no-handler
# ansible.builtin.blockinfile:
# marker: "## -- {tokens} ANSIBLE MANAGED BLOCK ##"
# block: "{{
# {
# 'tokens':{
# 'agent': _consul_agent_token.json.SecretID
# }
# } | to_nice_yaml
# }}"
# path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
# mode: "0644"
# when: "'OK' in _consul_agent_token.msg"
# - name: "Merge token configuration"
# delegate_to: localhost
# block:
# - name: "Stat consul secrets file"
# ansible.builtin.stat:
# path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
# register: _consul_config_file
#
# - name: "Load consul secrets file"
# ansible.builtin.include_vars:
# file: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
# name: _consul_cluster_config
# when: _consul_config_file.stat.exists
#
# - name: "Merge token configuration"
# vars:
# _config_to_merge:
# acl:
# tokens: "{{ _consul_cluster_config.tokens }}"
# ansible.builtin.set_fact:
# hashi_consul_configuration: "{{ hashi_consul_configuration | default({}) | combine(_config_to_merge, recursive=true) }}"
# when: _consul_cluster_config.tokens is defined
# - name: "Include ednz_cloud.hashistack.hashicorp_consul"
# ansible.builtin.include_role:
# name: ednz_cloud.hashistack.hashicorp_consul

View File

@ -41,7 +41,7 @@
_config_to_merge: _config_to_merge:
acl: acl:
tokens: tokens:
agent: "{{ _credentials.consul.tokens.agent }}" agent: "{{ _credentials.consul.tokens.agent.secret_id }}"
ansible.builtin.set_fact: ansible.builtin.set_fact:
hashi_consul_configuration: "{{ hashi_consul_configuration | default({}) | combine(_config_to_merge, recursive=true) }}" hashi_consul_configuration: "{{ hashi_consul_configuration | default({}) | combine(_config_to_merge, recursive=true) }}"

View File

@ -1,6 +1,43 @@
--- ---
- name: "Vault" - name: "Vault"
block: block:
- name: "Create consul token for service registration"
when:
- enable_consul
- vault_enable_service_registration
delegate_to: "{{ groups['consul_servers'] | first }}"
vars:
_consul_vault_sr_host: "{{ hostvars[groups['consul_servers'][0]].api_interface_address }}"
_consul_vault_sr_port: "{{ hostvars[groups['consul_servers'][0]].consul_api_port[hostvars[groups['consul_servers'][0]].consul_api_scheme] }}"
_consul_vault_sr_scheme: "{{ hostvars[groups['consul_servers'][0]].consul_api_scheme }}"
run_once: true
block:
- name: "Create consul vault policy"
community.general.consul_policy:
token: "{{ _credentials.consul.root_token.secret_id }}"
host: "{{ _consul_vault_sr_host }}"
port: "{{ _consul_vault_sr_port }}"
scheme: "{{ _consul_vault_sr_scheme }}"
validate_certs: false
state: present
name: vault-policy
rules: "{{ vault_service_registration_policy }}"
register: _consul_vault_policy
- name: "Create consul vault token"
community.general.consul_token:
token: "{{ _credentials.consul.root_token.secret_id }}"
host: "{{ _consul_vault_sr_host }}"
port: "{{ _consul_vault_sr_port }}"
scheme: "{{ _consul_vault_sr_scheme }}"
validate_certs: false
accessor_id: "{{ _credentials.consul.tokens.vault.accessor_id }}"
secret_id: "{{ _credentials.consul.tokens.vault.secret_id }}"
policies:
- id: "{{ _consul_vault_policy.policy.ID }}"
state: present
when: _consul_vault_policy.changed
- name: "Include ednz_cloud.hashistack.hashicorp_consul" - name: "Include ednz_cloud.hashistack.hashicorp_consul"
ansible.builtin.include_role: ansible.builtin.include_role:
name: ednz_cloud.hashistack.hashicorp_vault name: ednz_cloud.hashistack.hashicorp_vault