Reputation: 97
I know this is something I am doing wrong but its slowly driving me crazy. Please note, this cannot be yum controlled unfortunately. I have an ansible playbook that checks for the presence of tomcat, then contents of a file and retrieves a version:
Vars:
tomcat_version: 9.0.62
Checks:
- name: Check for Tomcat installation
stat:
path: "{{ tomcat_directory }}/lib/catalina.jar"
register: tomcat_already_installed
- name: Get existing Tomcat version
shell: "{{ java_directory }}/bin/java -cp {{ tomcat_directory }}/lib/catalina.jar org.apache.catalina.util.ServerInfo |grep \"Server number\" |awk -F':[[:blank:]]*' '{print $2}' | sed '/\\./ s/\\.\\{0,1\\}0\\{1,\\}$//'"
register: existing_tomcat_version
This returns a true / false and also a version number.
The next playbook will install Tomcat or Upgrade it... if the version (set in vars) isn't matched or Tomcat found.
- name: Install tomcat
include_tasks: install.yml
when:
The issue I have is with the when statement. Independently these two work:
# - not tomcat_already_installed
# - "existing_tomcat_version.stdout|string not in tomcat_version"
However, I've tried an or between them and it either doesn't install at all or just keeps running the installer every run. Things I've tried:
# - "not tomcat_already_installed or existing_tomcat_version.stdout|string not in tomcat_version"
# - not tomcat_already_installed or existing_tomcat_version.stdout|string not in tomcat_version
# - not tomcat_already_installed or 'existing_tomcat_version.stdout|string not in tomcat_version'
- not tomcat_already_installed or "existing_tomcat_version.stdout|string" not in tomcat_version
- "( not tomcat_already_installed or existing_tomcat_version.stdout|string not in tomcat_version"
Debug isn't really showing me anything other than the values look ok:
- name: Debug existing_tomcat_version.stdout
debug:
msg: "existing_tomcat_version = {{ existing_tomcat_version }}"
- name: Debug vars_tomcat_version
debug:
msg: "vars_tomcat_version = {{ tomcat_version }}"
ok: [i-0ddfdsfdsf90b8] => {
"msg": "existing_tomcat_version = 9.0.62"
}
Thursday 12 May 2022 15:38:27 +0200 (0:00:00.012) 0:00:38.711 **********
ok: [i-sdfdsfsdfsdffd] => {
"msg": "vars_tomcat_version = 9.0.62"
}
TASK [tomcat : debug tomcat_already_installed] *********************************************************************************************************************************************************************************************************************************
Thursday 12 May 2022 16:12:12 +0200 (0:00:00.915) 0:00:38.940 **********
Thursday 12 May 2022 16:12:12 +0200 (0:00:00.915) 0:00:38.939 **********
ok: [i-sdfsdfsdfsdfsdf] => {
"msg": "tomcat_already_installed = {'stat': {'exists': False}, 'changed': False, 'failed': False}"
Any help welcomed, I'm sure this is just a string misread or format thing. Thanks!
Upvotes: 0
Views: 106
Reputation: 44615
Just to put you on track, the following playbook:
---
- hosts: localhost
gather_facts: false
vars:
# Fake the original example vars from extra_vars on this example
existing_tomcat_version:
stdout: "{{ f_tomcat_version | d('0.0.0') }}"
tomcat_already_installed: "{{ f_tomcat_exists | bool }}"
tomcat_version: 9.0.62
tasks:
- debug:
msg: "Tomcat isn't installed or is not using version {{ tomcat_version }}"
when: not (tomcat_already_installed and existing_tomcat_version.stdout is version(tomcat_version, 'eq'))
Gives with different faked values passed as extra vars:
$ ansible-playbook playbook.yml -e f_tomcat_exists=false
PLAY [localhost] ****************************************************************************************************************************
TASK [debug] ********************************************************************************************************************************
ok: [localhost] => {
"msg": "Tomcat isn't installed or is not using version 9.0.62"
}
PLAY RECAP **********************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook playbook.yml -e f_tomcat_exists=true -e f_tomcat_version=8.4.5
PLAY [localhost] ****************************************************************************************************************************
TASK [debug] ********************************************************************************************************************************
ok: [localhost] => {
"msg": "Tomcat isn't installed or is not using version 9.0.62"
}
PLAY RECAP **********************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook playbook.yml -e f_tomcat_exists=true -e f_tomcat_version=9.0.62
PLAY [localhost] ****************************************************************************************************************************
TASK [debug] ********************************************************************************************************************************
skipping: [localhost]
PLAY RECAP **********************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Upvotes: 2