# Testing paths in conditionals without stat

**URL:** <https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980>\
**Category:** Guides, FAQs & Howtos\
**Tags:** playbook, howto\
**Created:** [April 16, 2024, 4:19pm UTC](https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980 "2024-04-16T16:19:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [April 16, 2024, 4:19pm UTC](https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980/1 "2024-04-16T16:19:33Z")

</div>

I thought I was quite familiar with Ansible conditionals, but I saw @bcoca do something I wasn’t aware of: testing paths without using `stat`:

```yaml
- hosts: localhost
  vars:
    builddir: "b1"
  tasks:
    - debug: msg="directory {{ builddir }} exists"
      when: builddir is exists and builddir is directory

```

It’s existed since 2.5, but I [only now did I get the memo](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_tests.html#testing-paths).

Edit: note that these are implemented as Jinja filters so their evaluation occurs on the controlling node.

I thought I’d mention it here as it’s getting a bit of feedback over on the Fediverse. 🙂

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 16, 2024, 4:38pm UTC](https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980/2 "2024-04-16T16:38:50Z")

</div>

Thanks, I’d also missed this, I think it could replace all of my uses of `stat` apart from the ones that need to check where a symlink points to or the checksum of a file. 👍

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 16, 2024, 5:55pm UTC](https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980/3 "2024-04-16T17:55:40Z")

</div>

TIL.

Tested with adhoc:

```bash
$ ansible localhost -i /dev/null -m debug -a "msg={{ 'test.yml' is exists }}"
localhost | SUCCESS => {
    "msg": true
}
$ ansible localhost -i /dev/null -m debug -a "msg={{ 'test.yml' is file }}"
localhost | SUCCESS => {
    "msg": true
}
$ ansible localhost -i /dev/null -m debug -a "msg={{ 'test.yml' is directory }}"
localhost | SUCCESS => {
    "msg": false
}

```

Testing against a remote host where I know test.yml doesn’t exist returns the same results though, so this appears to be a controller test, not a remote test. Unless I’m missing something, stat would still be required to test against remote inventory.

```bash
$ ansible testhost -m command -a "echo {{ 'test.yml' is exists }}"
testhost | CHANGED | rc=0 >>
True
$ ansible testhost -m stat -a "path=test.yml"
testhost | SUCCESS => {
    "changed": false,
    "stat": {
        "exists": false
    }
}

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 16, 2024, 7:13pm UTC](https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980/4 "2024-04-16T19:13:13Z")

</div>

> [@Denney-tech](#):
>
> this appears to be a controller test, not a remote test

Ahh, thanks for testing this, [the docs](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_tests.html#testing-paths) do make this clear, don’t know how I missed this 🙄

> information about a path on the control node

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [April 17, 2024, 5:23am UTC](https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980/5 "2024-04-17T05:23:23Z")

</div>

> [@Denney-tech](#):
>
> so this appears to be a controller test

These are actually Jinja filters, so yes – they occur on the controller, like all templating does. (Edited the above to add this.)

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [April 17, 2024, 4:19pm UTC](https://forum.ansible.com/t/testing-paths-in-conditionals-without-stat/4980/6 "2024-04-17T16:19:41Z")

</div>

> [@jpmens](#):
>
> These are actually Jinja filters

Tests, not filters. Filters are used with `|` (or `map()`) and are used to transform data in some way; tests are used with `is` (or `select()` etc.) and return true or false depending on whether the data passes the test.
