From 8262a1418db17828d6803e6363f7b21c414d28c4 Mon Sep 17 00:00:00 2001 From: Santiago Orobon Date: Thu, 14 May 2020 16:57:29 +0200 Subject: [PATCH] [#66] Improve agents management --- CHANGELOG.md | 1 + defaults/main.yml | 3 +- molecule/default/group_vars/tomcat/main.yml | 1 + tasks/agent.yml | 46 ++++++++++++++++----- templates/agents/version.txt.j2 | 1 + 5 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 templates/agents/version.txt.j2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ff2fb4..6731f5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a ch ### Changed - *[#61](https://github.com/idealista/tomcat_role/issues/61) Update molecule to 3.x* @sorobon - *[#63](https://github.com/idealista/tomcat_role/issues/63) Rename role to tomcat_role* @sorobon +- *[#66](https://github.com/idealista/tomcat_role/issues/66) Improve agents management* @sorobon ## [1.8.0](https://github.com/idealista/tomcat_role/tree/1.8.0) [Full Changelog](https://github.com/idealista/tomcat_role/compare/1.7.0...1.8.0) diff --git a/defaults/main.yml b/defaults/main.yml index 9fac430..041ae54 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -100,7 +100,7 @@ tomcat_default_log_logging_path: "${catalina.base}/logs" tomcat_default_log_valves: "logs" ## Agent configuration (optional) - +tomcat_agents_force_reinstall: false # tomcat_agents_required_libs: # - unzip # - apt-transport-https @@ -108,6 +108,7 @@ tomcat_default_log_valves: "logs" # tomcat_agents_config: # - name: "agent_name" # download_url: "download_url" +# version: "x.x.x" # catalina_opts: # - '-javaagent:{{ tomcat_install_path }}/agent_name/agent_file' # configuration_files: diff --git a/molecule/default/group_vars/tomcat/main.yml b/molecule/default/group_vars/tomcat/main.yml index 9cb7c5d..fafc50d 100644 --- a/molecule/default/group_vars/tomcat/main.yml +++ b/molecule/default/group_vars/tomcat/main.yml @@ -7,6 +7,7 @@ tomcat_agents_required_libs: tomcat_agents_config: - name: "newrelic" download_url: "http://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip" + version: "5.11.0" catalina_opts: - '-javaagent:{{ tomcat_install_path }}/newrelic/newrelic.jar' configuration_files: diff --git a/tasks/agent.yml b/tasks/agent.yml index e0b2deb..ffa9f64 100644 --- a/tasks/agent.yml +++ b/tasks/agent.yml @@ -1,12 +1,12 @@ --- -- name: Tomcat agent installation | Install required packages +- name: Tomcat | agent installation | Install required packages apt: pkg: "{{ item }}" state: present with_items: '{{ tomcat_agents_required_libs }}' -- name: Tomcat agent installation | Ensure install path +- name: Tomcat | agent installation | Ensure install path file: path: "{{ tomcat_install_path }}/{{ item.name }}" state: directory @@ -14,7 +14,7 @@ group: "{{ tomcat_group }}" with_items: '{{ tomcat_agents_config }}' -- name: Tomcat agent installation | Check if agent is installed +- name: Tomcat | agent installation | Check if agent is installed stat: path: "{{ tomcat_install_path }}/{{ item.0.name }}/{{ item.1 | basename }}" register: agent_file_exists @@ -22,16 +22,31 @@ - "{{ tomcat_agents_config }}" - catalina_opts -- name: Tomcat agent installation | Download agent +- name: Tomcat | agent installation | Check agent version + command: "cat {{ tomcat_install_path }}/{{ item.0.name }}/version.txt" + register: agent_version_checked + changed_when: false + ignore_errors: true + with_subelements: + - "{{ tomcat_agents_config }}" + - catalina_opts +- debug: + msg: "mierduza extrema {{ agent_version_checked }} " + +- name: Tomcat | agent installation | Download agent get_url: dest: "/tmp" url: "{{ item.download_url }}" owner: "{{ tomcat_user }}" group: "{{ tomcat_group }}" register: agent_downloaded - with_items: '{{ tomcat_agents_config }}' + loop: "{{ tomcat_agents_config|flatten(levels=1) }}" + loop_control: + index_var: index + when: tomcat_agents_force_reinstall or not agent_file_exists.results[index].stat.exists or item.version != agent_version_checked.results[index].stdout or agent_version_checked.results[index].failed + -- name: Tomcat agent installation | Unarchive package +- name: Tomcat | agent installation | Unarchive package unarchive: src: "/tmp/{{ item.download_url | basename }}" dest: "{{ tomcat_install_path }}" @@ -44,30 +59,41 @@ tags: - skip_ansible_lint -- name: Tomcat agent installation | Check configuration files +- name: Tomcat | agent installation | Copy version number file + template: + src: "agents/version.txt.j2" + dest: "{{ tomcat_install_path }}/{{ item.name }}/version.txt" + owner: "{{ tomcat_user }}" + group: "{{ tomcat_group }}" + mode: 0644 + with_items: '{{ tomcat_agents_config }}' + +- name: Tomcat | agent installation | Check configuration files set_fact: tomcat_agent_configuration: "{{ tomcat_agent_configuration|default([]) + [ {'name': item.0.name, 'file': item.1, 'params': item.0.params}] }}" with_subelements: - "{{ tomcat_agents_config }}" - configuration_files -- name: Tomcat agent installation | Configure the Java agent +- name: Tomcat | agent installation | Configure the Java agent template: src: "{{ playbook_dir }}/templates/tomcat/agents/{{ item.name }}/{{ item.file }}.j2" dest: "{{ tomcat_install_path }}/{{ item.name }}/{{ item.file }}" owner: "{{ tomcat_user }}" group: "{{ tomcat_group }}" + when: agent_downloaded.changed + notify: restart tomcat with_items: - '{{ tomcat_agent_configuration }}' -- name: Tomcat agent installation | Check catalina options +- name: Tomcat | agent installation | Check catalina options set_fact: tomcat_agent_catalina_opts: "{% set tomcat_agent_catalina_opts = tomcat_agent_catalina_opts|default([]) + [item.1] %}{{ tomcat_agent_catalina_opts|list }}" with_subelements: - "{{ tomcat_agents_config }}" - catalina_opts -- name: Tomcat agent installation | Configure catalina options +- name: Tomcat | agent installation | Configure catalina options template: src: "{{ item.src }}" dest: "{{ item.dest }}" diff --git a/templates/agents/version.txt.j2 b/templates/agents/version.txt.j2 new file mode 100644 index 0000000..208fd93 --- /dev/null +++ b/templates/agents/version.txt.j2 @@ -0,0 +1 @@ +{{ item.version }} \ No newline at end of file