# Switch Interface UP then playbook should end with error msg

**URL:** <https://forum.ansible.com/t/switch-interface-up-then-playbook-should-end-with-error-msg/36148>\
**Category:** Ansible Project\
**Created:** [December 23, 2021, 6:49am UTC](https://forum.ansible.com/t/switch-interface-up-then-playbook-should-end-with-error-msg/36148 "2021-12-23T06:49:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akshay\_Jadhav](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@Akshay\_Jadhav](https://forum.ansible.com/u/Akshay_Jadhav)\
**Post date:** [December 23, 2021, 6:49am UTC](https://forum.ansible.com/t/switch-interface-up-then-playbook-should-end-with-error-msg/36148/1 "2021-12-23T06:49:06Z")

</div>

Hi all,  
I need a help from you guys for writing a play if Switch Interface UP then playbook should end with error msg using when condition. I am kind of new to ansible. So please help me out for writing this task.  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 23, 2021, 6:52am UTC](https://forum.ansible.com/t/switch-interface-up-then-playbook-should-end-with-error-msg/36148/2 "2021-12-23T06:52:22Z")

</div>

What does your playbook look like?

---

<div class="post-metadata">

**Author:** ![Akshay\_Jadhav](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@Akshay\_Jadhav](https://forum.ansible.com/u/Akshay_Jadhav)\
**Post date:** [December 23, 2021, 10:54am UTC](https://forum.ansible.com/t/switch-interface-up-then-playbook-should-end-with-error-msg/36148/3 "2021-12-23T10:54:41Z")

</div>

- name: show message if interface is not down  
fail:  
msg: “Interface is not down”  
when: {{ interface }}.operstatus == “up”

will this work?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 23, 2021, 11:00am UTC](https://forum.ansible.com/t/switch-interface-up-then-playbook-should-end-with-error-msg/36148/4 "2021-12-23T11:00:27Z")

</div>

Probably not, but you could have tried yourself?

Drop the moustaches as when conditions are already evaluated as jinja

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 23, 2021, 1:29pm UTC](https://forum.ansible.com/t/switch-interface-up-then-playbook-should-end-with-error-msg/36148/5 "2021-12-23T13:29:27Z")

</div>

> ```
> \- name: show message if interface is not down
> fail:
> msg: &quot;Interface is not down&quot;
> when: \{\{ interface \}\}\.operstatus == &quot;up&quot;
> 
> ```
> 
> will this work?

Definitely not, that is pulled from thin air if you ask me.

Try that:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: vars.get('ansible\_' + interface).active

Note:

Gather facts saves the network interface information in separate variables (e.g. ansible\_eth0) instead of a dict/list,  
so accessing the info for a specific item is a bit awkward.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
