# Perform a task if there is a search result

**URL:** <https://forum.ansible.com/t/perform-a-task-if-there-is-a-search-result/5182>\
**Category:** Get Help\
**Created:** [April 27, 2024, 1:02pm UTC](https://forum.ansible.com/t/perform-a-task-if-there-is-a-search-result/5182 "2024-04-27T13:02:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![microlinux](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/microlinux/32/2968_2.png) [@microlinux](https://forum.ansible.com/u/microlinux)\
**Post date:** [April 27, 2024, 1:02pm UTC](https://forum.ansible.com/t/perform-a-task-if-there-is-a-search-result/5182/1 "2024-04-27T13:02:29Z")

</div>

Hi,

I’m using the following two tasks to replace the OpenJDK menu entry file on a Linux desktop. The file is a bit of a moving target, and its name sometimes changes from one release to the next.

```auto
# OpenJDK Policy Tool menu file is a moving target
- name: Find OpenJDK menu entry location
  ansible.builtin.find:
    paths: /usr/share/applications
    patterns: 'java*openjdk*.desktop'
  register: java_menufile

- name: Don't show OpenJDK Policy Tool in the menu
  ansible.builtin.copy:
    dest: "{{ java_menufile.files[0].path }}"
    content: |
      [Desktop Entry]
      NoDisplay=true
    owner: root
    group: root
    mode: 0644
  notify: Update desktop database

```

I’d like to add a little security here which says something like “don’t perform the second task if the search yields no results”.

I tried adding this:

```auto
when: java_menufile.stat.exists

```

But this doesn’t seem to work. I must be doing something wrong.

Any suggestions ?

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [April 27, 2024, 1:24pm UTC](https://forum.ansible.com/t/perform-a-task-if-there-is-a-search-result/5182/2 "2024-04-27T13:24:33Z")

</div>

> [@microlinux](#):
>
> `when: java_menufile.stat.exists`

The parameter in the registered variable `*.stats.exists` is for `ansible.builtin.stat` module.  
The structure of the registered variables are very different for each modules.

To get the structure of the returned variable, refer to the “Return Values” section in the docs for each modules.

- [ansible.builtin.stat module – Retrieve file or file system status — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html#return-values)
- [ansible.builtin.find module – Return a list of files based on specific criteria — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html#return-values)

Also dumping the registered variable is helpful to know its structure.

```yaml
- name: Dump registered variable
  ansible.builtin.debug:
    var: java_menufile

```

Anyway once you know the structure of the return value, you can consider how to make it a when condition. For your cases, perhaps

```yaml
when: java_menufile.matched

```

is the way.

---

<div class="post-metadata">

**Author:** ![microlinux](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/microlinux/32/2968_2.png) [@microlinux](https://forum.ansible.com/u/microlinux)\
**Post date:** [April 27, 2024, 1:53pm UTC](https://forum.ansible.com/t/perform-a-task-if-there-is-a-search-result/5182/3 "2024-04-27T13:53:47Z")

</div>

Thanks very much ! It works now.

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [April 27, 2024, 2:04pm UTC](https://forum.ansible.com/t/perform-a-task-if-there-is-a-search-result/5182/4 "2024-04-27T14:04:41Z")

</div>

Glad to hear that!  
To close this thread, could you please mark my post as a solution, if you don’t have any further questions? Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 27, 2024, 2:04pm UTC](https://forum.ansible.com/t/perform-a-task-if-there-is-a-search-result/5182/5 "2024-05-27T14:04:50Z")

</div>

Closed on May 27, 2024, 2:04pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
