From 7616a9aecb9de458887d556c298a639a80ccc1bb Mon Sep 17 00:00:00 2001 From: cicharka Date: Fri, 25 Mar 2022 15:48:34 +0100 Subject: [PATCH] * adjust service definition verification --- .../roles/kafka/files/jmx-kafka-config.yml | 2 +- .../kafka_exporter/tasks/upgrade/main.yml | 49 ++++++------------- .../upgrade/verify-service-definition.yml | 46 +++++++++++++++++ 3 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 ansible/playbooks/roles/kafka_exporter/tasks/upgrade/verify-service-definition.yml diff --git a/ansible/playbooks/roles/kafka/files/jmx-kafka-config.yml b/ansible/playbooks/roles/kafka/files/jmx-kafka-config.yml index 6a394fd80a..21b32cb214 100644 --- a/ansible/playbooks/roles/kafka/files/jmx-kafka-config.yml +++ b/ansible/playbooks/roles/kafka/files/jmx-kafka-config.yml @@ -85,4 +85,4 @@ rules: name: kafka_$1_$2_$3 type: GAUGE labels: - quantile: "0.$4" \ No newline at end of file + quantile: "0.$4" diff --git a/ansible/playbooks/roles/kafka_exporter/tasks/upgrade/main.yml b/ansible/playbooks/roles/kafka_exporter/tasks/upgrade/main.yml index c2b9692829..d262b42f5c 100644 --- a/ansible/playbooks/roles/kafka_exporter/tasks/upgrade/main.yml +++ b/ansible/playbooks/roles/kafka_exporter/tasks/upgrade/main.yml @@ -30,41 +30,10 @@ set_fact: kafka_exporter_version_old: "{{ kafka_exporter_installed_version.stdout }}" - # Between versions v1.3 and v2.0 there is no kafka_exporter version upgrade, but there is kafka upgrade - # For this reason kafka_exporter service definition needs to be updated - by simply enforing kafka_exporter upgrade - - name: Verify kafka version used in kafka_exporter service definition - block: - - name: Get installed Kafka version - shell: >- - set -o pipefail && - /opt/kafka/bin/kafka-server-start.sh --version | grep Commit | grep -oP '^\d+\.\d+\.\d+' - register: result - - - name: Set current_kafka_version fact - set_fact: - current_kafka_version: "{{ result.stdout }}" - - - name: Fetch kafka-exporter.service file from the remote - slurp: - src: /etc/systemd/system/kafka-exporter.service - register: kafka_exporter_service_definition - - - name: Parse kafka-exporter.service content - set_fact: - _exporter_service_definition_content: "{{ kafka_exporter_service_definition['content'] | b64decode | from_ini }}" - - - name: Get kafka version used in service definition - set_fact: - kafka_version_in_definition: "{{ _kafka_version_in_definition[0].split('=')[-1] }}" - vars: - _kafka_version_in_definition: - "{{ _exporter_service_definition_content['Service']['execstart'].split(' ') | select('search', 'kafka.version') }}" - - - name: Avoiding risk of downgrade - when: kafka_exporter_version_old is not version( kafka_exporter.version, '<' ) or - current_kafka_version is not version( kafka_version_in_definition, '!=') - debug: - msg: "Skipping upgrade: Kafka Exporter in the same or newer version already installed!" + - name: Avoiding risk of downgrade + when: kafka_exporter_version_old is not version( kafka_exporter.version, '<' ) + debug: + msg: "Skipping upgrade: Kafka Exporter in the same or newer version already installed!" - name: Run upgrade tasks when: lock_file_status.stat.exists or kafka_exporter_version_old is version( kafka_exporter.version, '<' ) @@ -132,3 +101,13 @@ file: path: "{{ lock_file }}" state: absent + + - name: Set kafka_exporter_upgraded flag + set_fact: + kafka_exporter_upgraded: True + +# Between versions v1.3 and v2.0 there is no kafka-exporter version upgrade, but there is kafka upgrade +# For this reason kafka-exporter service definition needs to be updated +- name: Verify if kafka-exporter service definition needs to be updated + include_tasks: upgrade/verify-service-definition.yml + when: kafka_exporter_upgraded is not defined diff --git a/ansible/playbooks/roles/kafka_exporter/tasks/upgrade/verify-service-definition.yml b/ansible/playbooks/roles/kafka_exporter/tasks/upgrade/verify-service-definition.yml new file mode 100644 index 0000000000..27ee46e2c1 --- /dev/null +++ b/ansible/playbooks/roles/kafka_exporter/tasks/upgrade/verify-service-definition.yml @@ -0,0 +1,46 @@ +--- +- name: Get installed Kafka version + shell: >- + set -o pipefail && + /opt/kafka/bin/kafka-server-start.sh --version | grep Commit | grep -oP '^\d+\.\d+\.\d+' + register: result + +- name: Set current_kafka_version fact + set_fact: + current_kafka_version: "{{ result.stdout }}" + +- name: Fetch kafka-exporter.service file from the remote + slurp: + src: /etc/systemd/system/kafka-exporter.service + register: kafka_exporter_service_definition + +- name: Parse kafka-exporter.service content + set_fact: + _exporter_service_definition_content: "{{ kafka_exporter_service_definition['content'] | b64decode | from_ini }}" + +- name: Get kafka version used in service definition + set_fact: + kafka_version_in_definition: "{{ _kafka_version_in_definition[0].split('=')[-1] }}" + vars: + _kafka_version_in_definition: + "{{ _exporter_service_definition_content['Service']['execstart'].split(' ') | select('search', 'kafka.version') }}" + +- name: Update service definition + when: current_kafka_version is not version( kafka_version_in_definition, '==') + block: + - name: Update service + template: + src: kafka-exporter.service.j2 + dest: /etc/systemd/system/kafka-exporter.service + owner: root + group: root + mode: u=rw,go=r + + - name: Restart kafka-exporter service + service: + name: kafka-exporter + state: restarted + + - name: Reload systemd daemons + systemd: + daemon_reload: true