From 00388ad49ca5be9ec767fa4c36c2389c31b09955 Mon Sep 17 00:00:00 2001 From: Jack Zhang Date: Sat, 26 Oct 2024 21:57:50 +0800 Subject: [PATCH] use flatten variables --- defaults/main.yml | 8 +++++--- molecule/default/verify.yml | 26 ++++++++++++++++++++++++++ templates/override.conf.j2 | 15 ++++++++++++--- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 molecule/default/verify.yml diff --git a/defaults/main.yml b/defaults/main.yml index 1d91147..6aa5457 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -5,9 +5,11 @@ jenkins_version: "2.452.4" jenkins_process_user: jenkins jenkins_process_group: "{{ jenkins_process_user }}" -jenkins_systemd_unit_envs: - java_opts: "-Djenkins.install.runSetupWizard=false" - jenkins_listen_address: 127.0.0.1 +# Jenkins systemd unit override.conf +jenkins_java_home: "" +jenkins_java_opts: "-Djenkins.install.runSetupWizard=false" +jenkins_listen_address: 127.0.0.1 +jenkins_secrets_file: "" jenkins_home: /var/lib/jenkins diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml new file mode 100644 index 0000000..e6fb378 --- /dev/null +++ b/molecule/default/verify.yml @@ -0,0 +1,26 @@ +--- +- name: Verify Jenkins systemd unit environment variables + hosts: all + tasks: + - name: Assert JAVA_OPTS is defined in override.conf + ansible.builtin.slurp: + src: /etc/systemd/system/jenkins.service.d/override.conf # Adjust the path as necessary + register: override_conf_content + + - name: Decode override.conf content + ansible.builtin.set_fact: + override_conf_decoded: "{{ override_conf_content.content | b64decode }}" + + - name: Assert JAVA_OPTS is present + ansible.builtin.assert: + that: + - override_conf_decoded is defined + - override_conf_decoded | search('JAVA_OPTS={{ jenkins_java_opts }}') + fail_msg: "JAVA_OPTS is not set correctly in override.conf." + + - name: Assert JENKINS_LISTEN_ADDRESS is present + ansible.builtin.assert: + that: + - override_conf_decoded is defined + - override_conf_decoded | search('JENKINS_LISTEN_ADDRESS={{ jenkins_listen_address }}') + fail_msg: "JENKINS_LISTEN_ADDRESS is not set correctly in override.conf." diff --git a/templates/override.conf.j2 b/templates/override.conf.j2 index 25cf313..c5d806b 100644 --- a/templates/override.conf.j2 +++ b/templates/override.conf.j2 @@ -1,4 +1,13 @@ [Service] -{% for key, value in jenkins_systemd_unit_envs.items() %} -Environment="{{ key | upper }}={{ value }}" -{% endfor %} +{% if jenkins_java_home is defined %} +Environment="JAVA_HOME={{ jenkins_java_home }}" +{% endif %} +{% if jenkins_java_opts is defined %} +Environment="JAVA_OPTS={{ jenkins_java_opts }}" +{% endif %} +{% if jenkins_listen_address is defined %} +Environment="JENKINS_LISTEN_ADDRESS={{ jenkins_listen_address }}" +{% endif %} +{% if jenkins_secrets_file is defined %} +Environment="SECRETS_FILE={{ jenkins_secrets_file }}" +{% endif %}