Compare commits

..

No commits in common. "b66113b84cf258f6c19536d612210be3b88057fe" and "4e0c90f11013576a30ba1017cf1df577bec87c64" have entirely different histories.

4 changed files with 94 additions and 86 deletions

View File

@ -1,5 +1,5 @@
--- ---
# hashistack generate certificates playbook # hashistack deployment playbook
- name: "Generate certificates" - name: "Generate certificates"
hosts: all hosts: all
strategy: linear strategy: linear
@ -272,4 +272,4 @@
group: "{{ lookup('env', 'USER') }}" group: "{{ lookup('env', 'USER') }}"
mode: "0644" mode: "0644"
# - fail: # - fail:

View File

@ -10,24 +10,22 @@
block: block:
- name: "Generate consul root credentials" - name: "Generate consul root credentials"
ansible.builtin.set_fact: ansible.builtin.set_fact:
_consul_root_token: "{{ lookup('password', '/dev/null chars=ascii_letters,digits') | to_uuid }}" _consul_root_token: "{{ lookup('uuid', namespace='00000000-0000-0000-0000-000000000000') }}"
- name: "Generate consul agents credentials" - name: "Generate consul agents credentials"
ansible.builtin.set_fact: ansible.builtin.set_fact:
_consul_agents_token: "{{ lookup('password', '/dev/null chars=ascii_letters,digits') | to_uuid }}" _consul_agents_token: "{{ lookup('uuid', namespace='00000000-0000-0000-0000-000000000000') }}"
- name: "Generate nomad credentials" - name: "Generate nomad credentials"
block: block:
- name: "Generate nomad root credentials" - name: "Generate nomad root credentials"
ansible.builtin.set_fact: ansible.builtin.set_fact:
_nomad_root_token: "{{ lookup('password', '/dev/null chars=ascii_letters,digits') | to_uuid }}" _nomad_root_token: "{{ lookup('uuid', namespace='00000000-0000-0000-0000-000000000000') }}"
- name: "Write credentials file" - name: "Write credentials file"
ansible.builtin.template: ansible.builtin.template:
src: templates/credentials.yml.j2 src: credentials.yml.j2
dest: "{{ sub_configuration_directories['secrets'] }}/{{ configuration_credentials_vars_file }}" dest: credentials.yml
owner: "{{ lookup('env', 'USER') }}"
group: "{{ lookup('env', 'USER') }}"
mode: '0644' mode: '0644'
# - fail: - fail:

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" # TODO: this should be dynamic (http/https) url: "http://{{ api_interface_address }}:8500"
validate_certs: no validate_certs: no
return_content: yes return_content: yes
status_code: status_code:
@ -19,7 +19,6 @@
- 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
@ -29,27 +28,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:
@ -58,14 +57,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:
token: "{{ _credentials.consul.root_token.secret_id }}" host: "{{ hashi_consul_configuration['advertise_addr'] }}"
host: "{{ api_interface_address }}" token: "{{ _consul_cluster_config.root_token.secret_id }}"
port: 8500 port: 8500
scheme: http # TODO: this should be dynamic scheme: http
validate_certs: false
state: present state: present
name: agents-policy name: agents-policy
rules: "{{ consul_default_agent_policy }}" rules: "{{ consul_default_agent_policy }}"
@ -75,65 +74,55 @@
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: "{{ _credentials.consul.root_token.secret_id }}" X-Consul-Token: "{{ _consul_cluster_config.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: "Restart consul service" - name: "Write consul agents token to file" # no-handler
ansible.builtin.service: ansible.builtin.blockinfile:
name: "consul_container" marker: "## -- {tokens} ANSIBLE MANAGED BLOCK ##"
state: restarted block: "{{
throttle: 1 {
when: _consul_agent_token.changed '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: "Write consul agents token to file" # no-handler - name: "Merge token configuration"
# ansible.builtin.blockinfile: delegate_to: localhost
# marker: "## -- {tokens} ANSIBLE MANAGED BLOCK ##" block:
# block: "{{ - name: "Stat consul secrets file"
# { ansible.builtin.stat:
# 'tokens':{ path: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
# 'agent': _consul_agent_token.json.SecretID register: _consul_config_file
# }
# } | 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: "Load consul secrets file"
# delegate_to: localhost ansible.builtin.include_vars:
# block: file: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
# - name: "Stat consul secrets file" name: _consul_cluster_config
# ansible.builtin.stat: when: _consul_config_file.stat.exists
# 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" - name: "Merge token configuration"
# ansible.builtin.include_role: vars:
# name: ednz_cloud.hashistack.hashicorp_consul _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

@ -36,14 +36,35 @@
- 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: tokens: "{{ _consul_cluster_config.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: