feat(consul): utilize the new pre-generated credentials on consul deployment

This commit is contained in:
Bertrand Lanson 2024-05-03 22:26:30 +02:00
parent 4e0c90f110
commit 0a1ddc63bf
2 changed files with 76 additions and 86 deletions

View File

@ -7,7 +7,7 @@
- name: "Wait for consul cluster to initialize" # noqa: run-once[task] - name: "Wait for consul cluster to initialize" # noqa: run-once[task]
ansible.builtin.uri: ansible.builtin.uri:
url: "http://{{ api_interface_address }}:8500" url: "http://{{ api_interface_address }}:8500" # TODO: this should be dynamic (http/https)
validate_certs: no validate_certs: no
return_content: yes return_content: yes
status_code: status_code:
@ -19,6 +19,7 @@
- name: "Initialize consul cluster" # noqa: run-once[task] - name: "Initialize consul cluster" # noqa: run-once[task]
community.general.consul_acl_bootstrap: community.general.consul_acl_bootstrap:
bootstrap_secret: "{{ _credentials.consul.root_token.secret_id }}"
host: "{{ hashi_consul_configuration['advertise_addr'] }}" host: "{{ hashi_consul_configuration['advertise_addr'] }}"
port: 8500 port: 8500
scheme: http scheme: http
@ -28,27 +29,27 @@
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 # - name: "Write consul configuration to file" # noqa: run-once[task] no-handler
ansible.builtin.blockinfile: # ansible.builtin.blockinfile:
marker: "## -- {root_token} ANSIBLE MANAGED BLOCK ##" # marker: "## -- {root_token} ANSIBLE MANAGED BLOCK ##"
block: "{{ # block: "{{
{ # {
'root_token':{ # 'root_token':{
'accessor_id': _consul_init_secret.result.AccessorID, # 'accessor_id': _consul_init_secret.result.AccessorID,
'secret_id': _consul_init_secret.result.SecretID # 'secret_id': _consul_init_secret.result.SecretID
} # }
} | to_nice_yaml # } | to_nice_yaml
}}" # }}"
path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml" # path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
mode: "0644" # mode: "0644"
when: _consul_init_secret.result is defined # when: _consul_init_secret.result is defined
run_once: true # run_once: true
delegate_to: localhost # delegate_to: localhost
- name: "Load consul cluster variables" # - name: "Load consul cluster variables"
ansible.builtin.include_vars: # ansible.builtin.include_vars:
file: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml" # file: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
name: _consul_cluster_config # name: _consul_cluster_config
- name: "Create consul agents token" - name: "Create consul agents token"
when: when:
@ -57,14 +58,14 @@
block: block:
- name: "Create consul agents token" # noqa: run-once[task] no-handler - name: "Create consul agents token" # noqa: run-once[task] no-handler
run_once: true run_once: true
delegate_to: localhost
block: block:
- name: "Create consul agent policy" - name: "Create consul agent policy"
community.general.consul_policy: community.general.consul_policy:
host: "{{ hashi_consul_configuration['advertise_addr'] }}" token: "{{ _credentials.consul.root_token.secret_id }}"
token: "{{ _consul_cluster_config.root_token.secret_id }}" host: "{{ api_interface_address }}"
port: 8500 port: 8500
scheme: http scheme: http # TODO: this should be dynamic
validate_certs: false
state: present state: present
name: agents-policy name: agents-policy
rules: "{{ consul_default_agent_policy }}" rules: "{{ consul_default_agent_policy }}"
@ -74,55 +75,65 @@
vars: vars:
consul_full_url: "http://{{ hashi_consul_configuration['advertise_addr'] }}:8500" consul_full_url: "http://{{ hashi_consul_configuration['advertise_addr'] }}:8500"
consul_token_body: consul_token_body:
Description: "Consul agents token"
SecretID: "{{ _credentials.consul.tokens.agent }}"
Policies: Policies:
- ID: "{{ _consul_agent_policy.policy.ID }}" - ID: "{{ _consul_agent_policy.policy.ID }}"
ansible.builtin.uri: ansible.builtin.uri:
url: "{{ consul_full_url }}/v1/acl/token" url: "{{ consul_full_url }}/v1/acl/token"
method: PUT method: PUT
headers: headers:
X-Consul-Token: "{{ _consul_cluster_config.root_token.secret_id }}" X-Consul-Token: "{{ _credentials.consul.root_token.secret_id }}"
body: "{{ consul_token_body | to_json }}" body: "{{ consul_token_body | to_json }}"
status_code: status_code:
- 200 - 200
register: _consul_agent_token register: _consul_agent_token
changed_when: _consul_agent_token.status == 200
- name: "Write consul agents token to file" # no-handler - name: "Restart consul service"
ansible.builtin.blockinfile: ansible.builtin.service:
marker: "## -- {tokens} ANSIBLE MANAGED BLOCK ##" name: "consul_container"
block: "{{ state: restarted
{ throttle: 1
'tokens':{ when: _consul_agent_token.changed
'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" # - name: "Write consul agents token to file" # no-handler
delegate_to: localhost # ansible.builtin.blockinfile:
block: # marker: "## -- {tokens} ANSIBLE MANAGED BLOCK ##"
- name: "Stat consul secrets file" # block: "{{
ansible.builtin.stat: # {
path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml" # 'tokens':{
register: _consul_config_file # '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: "Load consul secrets file" # - name: "Merge token configuration"
ansible.builtin.include_vars: # delegate_to: localhost
file: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml" # block:
name: _consul_cluster_config # - name: "Stat consul secrets file"
when: _consul_config_file.stat.exists # 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: "Merge token configuration" # - name: "Include ednz_cloud.hashistack.hashicorp_consul"
vars: # ansible.builtin.include_role:
_config_to_merge: # name: ednz_cloud.hashistack.hashicorp_consul
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

@ -36,35 +36,14 @@
- name: "Consul | Merge token configuration" - name: "Consul | Merge token configuration"
delegate_to: localhost delegate_to: localhost
block: block:
- name: "Consul | Stat consul secrets file"
ansible.builtin.stat:
path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
register: _consul_config_file
- name: "Consul | 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: "Consul | Merge token configuration" - name: "Consul | Merge token configuration"
vars: vars:
_config_to_merge: _config_to_merge:
acl: acl:
tokens: "{{ _consul_cluster_config.tokens }}" tokens:
agent: "{{ _credentials.consul.tokens.agent }}"
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) }}"
when: _consul_cluster_config.tokens is defined
- name: "Consul | Create secrets file if not present"
ansible.builtin.file:
path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
state: touch
owner: "{{ lookup('env', 'USER') }}"
group: "{{ lookup('env', 'USER') }}"
mode: "0644"
run_once: true
when: not _consul_config_file.stat.exists
- name: "Consul | Merge extra configuration settings" - name: "Consul | Merge extra configuration settings"
vars: vars: