Help with conditionals syntax

I know my syntax is wrong but you can get the gist with the code below. Can someone correct my invalid syntax?

Works:

when: ansible_hostname | search("apps")

Fails:

`
when: ansible_hostname | search(“apps”) and
ansible_hostname != bad_server|bad_server2

`

anyone?

when:

  • ansible_hostname | search(“apps”)
  • ansible_hostname not in [“bad_server”, “bad_server2”]

Note: using a list format for when is an implicit AND

Thank you! I didn’t know a list was an implicit AND. I tested it and it works. I appreciate you taking the time to help me out (and teach me something too)!

One follow-up question… how could I do this with a wildcard? Also, are these jinja2? I am trying to find documentation to guide me, but don’t know what to look for.

`

when:

  • ansible_hostname | search(“apps”)
  • ansible_hostname not in [“*_server”]

`

They are jinja. You cannot do a wildcard like that. You would instead want to use search again, but negate the evaluation:

  • not ansible_hostname | search(‘.*_server’)

Thanks again Matt.