Ansible task for searching words

ansiblegrepless

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?

Best Answer

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 != ""
Related Question