# Link conditions with or under failed\_when:

**URL:** <https://forum.ansible.com/t/link-conditions-with-or-under-failed-when/226>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [August 31, 2023, 8:21am UTC](https://forum.ansible.com/t/link-conditions-with-or-under-failed-when/226 "2023-08-31T08:21:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![myservertechnik](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@myservertechnik](https://forum.ansible.com/u/myservertechnik)\
**Post date:** [August 31, 2023, 8:21am UTC](https://forum.ansible.com/t/link-conditions-with-or-under-failed-when/226/1 "2023-08-31T08:21:54Z")

</div>

Hello Community,

i am trying to verify my FQDN in a playbook against some subdomains.

```auto
  tasks: 
 - name: Check Hostname
   debug:
     var: ansible_fqdn
   failed_when: "'.1.domain.local' or '.2.domain.local' or '.3.domain.local' not in ansible_fqdn"

```

Where is my mistake?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![relrod](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/relrod/32/107_2.png) [@relrod](https://forum.ansible.com/u/relrod)\
**Post date:** [August 31, 2023, 8:47am UTC](https://forum.ansible.com/t/link-conditions-with-or-under-failed-when/226/2 "2023-08-31T08:47:46Z")

</div>

Hi @myservertechnik and welcome to the forum.

For future reference you can use three backticks (```) around your code and it will make it easier to read (monospace font and it avoids markdown stuff like converting the initial dash to a list bullet).

Also, it would be helpful in the future to include the error you are seeing, and what you are expecting to see.

In this case, the problem is almost certainly your `failed_when` line.

This uses a Python-style (Jinja2) syntax, and so your line is getting parsed like this:

```yaml
failed_when: "('.1.domain.local') or ('.2.domain.local') or ('.3.domain.local' not in ansible_fqdn)"

```

> **And non-empty strings are truthy...**
>
> ```auto
> $ ansible -m debug -a "msg={{ 'true' if 'I am nonempty' else 'false' }}" localhost
> localhost | SUCCESS => {
> "msg": "true"
> }
> 
> ```

…so your expression simplifies to:

`failed_when: True or True or ('.3.domain.local' not in ansible_fqdn)`

You’d want to use a full boolean expression for each of these. For example:

```yaml
failed_when: "'.1.domain.local' not in ansible_fqdn or '.2.domain.local' not in ansible_fqdn or '.3.domain.local' not in ansible_fqdn"

```

However, you might also consider using a [regex test](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/regex_test.html) here instead.

Perhaps something like this could work:

```yaml
failed_when: ansible_fqdn is not regex('\.[1-3]\.domain\.local')

```

Example:

```auto
$ ansible -m debug -a "msg={{ 'foo.1.domain.local' is regex('\.[1-3]\.domain\.local') }}" localhost
localhost | SUCCESS => {
    "msg": true
}

$ ansible -m debug -a "msg={{ 'foo.99.domain.local' is regex('\.[1-3]\.domain\.local') }}" localhost
localhost | SUCCESS => {
    "msg": false
}

```

---

<div class="post-metadata">

**Author:** ![IPvSean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ipvsean/32/1153_2.png) [@IPvSean](https://forum.ansible.com/u/IPvSean)\
**Post date:** [August 31, 2023, 1:06pm UTC](https://forum.ansible.com/t/link-conditions-with-or-under-failed-when/226/3 "2023-08-31T13:06:36Z")

</div>

going further you could also make it multi-line similar to `and` statements by just adding the `or` at the end. In fact ansible-lint might encourage you do this with certain line lengths

```auto
when: >
  "'.1.domain.local' not in ansible_fqdn or
  '.2.domain.local' not in ansible_fqdn or
  .3.domain.local' not in ansible_fqdn"

```

have not tested the above, but should be close to correct

---

<div class="post-metadata">

**Author:** ![relrod](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/relrod/32/107_2.png) [@relrod](https://forum.ansible.com/u/relrod)\
**Post date:** [August 31, 2023, 5:58pm UTC](https://forum.ansible.com/t/link-conditions-with-or-under-failed-when/226/4 "2023-08-31T17:58:33Z")

</div>

Yep, also a valid solution, though you’d want to remove the outer quotes (the `>` makes them redundant and they will likely break the expression since it is just getting evaluated to a string - I’m not sure if ansible will reject it or just evaluate it to `True`).
