Dave Shaw
Dave Shaw

Reputation: 97

Ansible, set fact when yum package is installed

I'm a little stumped by this one. I'm attempting to script a full oracle installation, I've got all the OSB Suite and other parts sorted but the JDK is driving me crazy. The validate runs but I cannot get it set a fact correctly. I'm definitely missing something simple.

vars/main:

# vars for jdk
java_jdk_already_installed: false
java_jdk_installer: "jdk-8u271-linux-x64.rpm"
java_jdk_definition: 'jdk1.8.x86_64'

tasks/validate:

- name: "validate java installation {{ java_jdk_installer }}"
  yum:
    list: "{{ java_jdk_definition }}"
    disable_gpg_check: yes
  register: java_yum_output

- debug:
    msg: "java state {{ java_yum_output }}"

- set_fact:
    java_jdk_already_installed: true
  when: java_yum_output|length > 0

Debug output with the package present:

ok: [localhost] => {
    "msg": "java state {'msg': '', 'results': [{'name': 'jdk1.8', 'arch': 'x86_64', 'epoch': '2000', 'release': 'fcs', 'version': '1.8.0_271', 'repo': '@System', 'nevra': '2000:jdk1.8-1.8.0_271-fcs.x86_64', 'yumstate': 'installed'}], 'failed': False, 'changed': False}"
}

Debug with the package missing:

ok: [localhost] => {
    "msg": "java state {'msg': '', 'results': [], 'failed': False, 'changed': False}"
}

I can get it to skip if I use

when: java_yum_output.results[0].yumstate == "installed"

But if the package isn't present, the list has no elements and errors. Can someone help?

Thanks!

Upvotes: 1

Views: 589

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68179

Try

    - set_fact:
        java_jdk_already_installed: true
      when: java_yum_output.results.0.yumstate|default('') == "installed"

Upvotes: 1

Related Questions