1

With my ansible task I wanted to check if openjdk is installed on environment.

- name: Check if java is installed on environment
   shell: rpm -qa | grep 'openjdk'
   register: result

As result I got failure, because grep didn't find anything and return code was 1.

TASK [xms_webapp : Check if java is installed on environment] ****************** fatal: [10.230.14.21]: FAILED! => {"changed": true, "cmd": "rpm -qa | grep 'openjdk'", "delta": "0:00:00.721086", "end": "2019-02-20 12:17:40.253171", "msg": "non-zero return code", "rc": 1, "start": "2019-02-20 12:17:39.532085", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []} to retry, use: --limit @/opt/ngpe/share/playbooks/xms/xms.retry

As alternative way, I'm trying use less command and his -p option to use pattern.

rpm -qa > /tmp/openjdk.txt | less -p /'\w[openjdk]' /tmp/openjdk.txt

But the result is "Pattern not found".

When I open file with vim and type: /openjdk

I find out that there is wanted word.

plymouth-scripts-0.8.3-29.el6.x86_64
lm_sensors-libs-3.1.1-17.el6.x86_64
vim-minimal-7.4.629-5.el6_8.1.x86_64
adl-release-13-03.00.09.el6.noarch
lua-5.1.4-4.1.el6.x86_64
adl-pmc3-3.1-04.00.14.el6.x86_64
libidn-1.18-2.el6.x86_64
module-init-tools-3.9-26.el6.x86_64
libpciaccess-0.13.4-1.el6.x86_64
libgpg-error-1.7-4.el6.x86_64
java-1.8.0-**openjdk**-1.8.0.191.b12-0.el6_10.x86_64
cpio-2.10-13.el6.x86_64
p11-kit-0.18.5-2.el6.x86_64
cvs-1.11.23-16.el6.x86_64
libutempter-1.1.5-4.1.el6.x86_64
checkpolicy-2.0.22-1.el6.x86_64
perl-Expect-1.21-3.el6.noarch
which-2.19-6.el6.x86_64
hpacucli-9.40-12.0.x86_64
cma_server-1.2-00.B03.noarch
pth-2.0.7-9.3.el6.x86_64
p11-kit-trust-0.18.5-2.el6.x86_64
oddjob-0.30-5.el6.x86_64

Could someone tell me what is wrong?

  • Just to minimize distraction, note that in your manual example, running rpm -qa > /tmp/openjdk.txt | less ... will send all the output you're looking for into the temp file, leaving nothing for less to look at. – Jeff Schaller Feb 20 at 15:09
  • Aaah, thanks, I see it now. Hmm, so I need to divide it into two tasks... – rusna Feb 20 at 15:19
  • I wasn't sure if your main goal was the rpm/less aspect or the Ansible syntax. – Jeff Schaller Feb 20 at 15:19
  • No problem, I should clarified it better. – rusna Feb 20 at 15:24
  • 1
    If you've found a solution to your problem, the right thing to do is to put that answer down in the Answer box, below. Thank you! – Jeff Schaller Feb 20 at 15:36
0

I solved this problem, by adding ignore_errors: yes in ansible task.

- name: Check if java is installed on environment
   shell: rpm -qa | grep 'openjdk'
   ignore_errors: yes
   register: result

Because my subsequent task is checking registered result.

- name: Exclude java from updating to newer version, if java is installed
   lineinfile:
     path: /etc/yum.conf
     line: exclude=java*
     insertafter: [main]
   when: result.stdout != ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.