2024-04-01 21:59:48 +00:00
|
|
|
---
|
2024-03-13 22:27:12 +00:00
|
|
|
- name: "Consul"
|
|
|
|
block:
|
|
|
|
- name: "Include ednz_cloud.hashistack.hashicorp_consul"
|
|
|
|
ansible.builtin.include_role:
|
|
|
|
name: ednz_cloud.hashistack.hashicorp_consul
|
|
|
|
|
|
|
|
- name: "Wait for consul cluster to initialize" # noqa: run-once[task]
|
2024-04-01 21:59:48 +00:00
|
|
|
ansible.builtin.uri:
|
2024-05-05 00:37:29 +00:00
|
|
|
url: "{{ consul_api_addr }}"
|
2024-04-01 21:59:48 +00:00
|
|
|
validate_certs: no
|
|
|
|
return_content: yes
|
|
|
|
status_code:
|
|
|
|
- 200
|
|
|
|
until: uri_output.status == 200
|
|
|
|
retries: 24
|
|
|
|
delay: 5
|
|
|
|
register: uri_output
|
2024-03-13 22:27:12 +00:00
|
|
|
|
|
|
|
- name: "Initialize consul cluster" # noqa: run-once[task]
|
|
|
|
community.general.consul_acl_bootstrap:
|
2024-05-03 20:26:30 +00:00
|
|
|
bootstrap_secret: "{{ _credentials.consul.root_token.secret_id }}"
|
2024-05-05 00:37:29 +00:00
|
|
|
host: "{{ api_interface_address }}"
|
|
|
|
port: "{{ consul_api_port[consul_api_scheme] }}"
|
|
|
|
scheme: "{{ consul_api_scheme }}"
|
2024-03-13 22:27:12 +00:00
|
|
|
state: present
|
|
|
|
run_once: true
|
|
|
|
delegate_to: "{{ groups['consul_servers'] | first }}"
|
|
|
|
register: _consul_init_secret
|
|
|
|
when: hashi_consul_configuration.acl.enabled
|
|
|
|
|
2024-05-03 20:26:30 +00:00
|
|
|
# - 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
|
2024-03-13 22:27:12 +00:00
|
|
|
|
2024-05-03 20:26:30 +00:00
|
|
|
# - name: "Load consul cluster variables"
|
|
|
|
# ansible.builtin.include_vars:
|
|
|
|
# file: "{{ sub_configuration_directories.consul_servers }}/consul_config.yml"
|
|
|
|
# name: _consul_cluster_config
|
2024-03-13 22:27:12 +00:00
|
|
|
|
|
|
|
- name: "Create consul agents token"
|
|
|
|
when:
|
|
|
|
- _consul_init_secret.changed
|
|
|
|
- consul_acl_configuration.enabled
|
|
|
|
block:
|
|
|
|
- name: "Create consul agents token" # noqa: run-once[task] no-handler
|
|
|
|
run_once: true
|
|
|
|
block:
|
|
|
|
- name: "Create consul agent policy"
|
|
|
|
community.general.consul_policy:
|
2024-05-03 20:26:30 +00:00
|
|
|
token: "{{ _credentials.consul.root_token.secret_id }}"
|
|
|
|
host: "{{ api_interface_address }}"
|
2024-05-05 00:37:29 +00:00
|
|
|
port: "{{ consul_api_port[consul_api_scheme] }}"
|
|
|
|
scheme: "{{ consul_api_scheme }}"
|
2024-05-03 20:26:30 +00:00
|
|
|
validate_certs: false
|
2024-03-13 22:27:12 +00:00
|
|
|
state: present
|
|
|
|
name: agents-policy
|
|
|
|
rules: "{{ consul_default_agent_policy }}"
|
|
|
|
register: _consul_agent_policy
|
|
|
|
|
|
|
|
- name: "Create consul agents token"
|
|
|
|
vars:
|
|
|
|
consul_token_body:
|
2024-05-03 20:26:30 +00:00
|
|
|
Description: "Consul agents token"
|
|
|
|
SecretID: "{{ _credentials.consul.tokens.agent }}"
|
2024-03-13 22:27:12 +00:00
|
|
|
Policies:
|
|
|
|
- ID: "{{ _consul_agent_policy.policy.ID }}"
|
|
|
|
ansible.builtin.uri:
|
2024-05-05 00:37:29 +00:00
|
|
|
url: "{{ consul_api_addr }}/v1/acl/token"
|
2024-03-13 22:27:12 +00:00
|
|
|
method: PUT
|
|
|
|
headers:
|
2024-05-03 20:26:30 +00:00
|
|
|
X-Consul-Token: "{{ _credentials.consul.root_token.secret_id }}"
|
2024-03-13 22:27:12 +00:00
|
|
|
body: "{{ consul_token_body | to_json }}"
|
|
|
|
status_code:
|
|
|
|
- 200
|
|
|
|
register: _consul_agent_token
|
2024-05-03 20:26:30 +00:00
|
|
|
changed_when: _consul_agent_token.status == 200
|
2024-03-13 22:27:12 +00:00
|
|
|
|
2024-05-03 20:26:30 +00:00
|
|
|
- name: "Restart consul service"
|
|
|
|
ansible.builtin.service:
|
|
|
|
name: "consul_container"
|
|
|
|
state: restarted
|
|
|
|
throttle: 1
|
|
|
|
when: _consul_agent_token.changed
|
2024-03-13 22:27:12 +00:00
|
|
|
|
2024-05-03 20:26:30 +00:00
|
|
|
# - 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"
|
2024-03-13 22:27:12 +00:00
|
|
|
|
2024-05-03 20:26:30 +00:00
|
|
|
# - 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
|
2024-03-13 22:27:12 +00:00
|
|
|
|
2024-05-03 20:26:30 +00:00
|
|
|
# - name: "Include ednz_cloud.hashistack.hashicorp_consul"
|
|
|
|
# ansible.builtin.include_role:
|
|
|
|
# name: ednz_cloud.hashistack.hashicorp_consul
|