# don't print statement if "command not found"  and compare variable value with list of values

**URL:** <https://forum.ansible.com/t/dont-print-statement-if-command-not-found-and-compare-variable-value-with-list-of-values/29050>\
**Category:** Ansible Project\
**Created:** [November 16, 2018, 2:06pm UTC](https://forum.ansible.com/t/dont-print-statement-if-command-not-found-and-compare-variable-value-with-list-of-values/29050 "2018-11-16T14:06:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![moissetgerard0](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@moissetgerard0](https://forum.ansible.com/u/moissetgerard0)\
**Post date:** [November 16, 2018, 2:06pm UTC](https://forum.ansible.com/t/dont-print-statement-if-command-not-found-and-compare-variable-value-with-list-of-values/29050/1 "2018-11-16T14:06:40Z")

</div>

i’m trying this in a playbook:

…  
vars:  
actionTodo: “{{ variable\_actionTodo }}”  
servicesList: “{{ variable\_servicesList }}”  
actionTodo\_list: [start,stop,rstart,rstop,status,rstatus]  
…

- name: manage services using systemctl  
tags: manage services using systemctl  
shell: systemctl {{actionTodo}} {{servicesList}}  
with\_items:

- “{{servicesList}}”  
when: actionTodo != “” and servicesList != “”  
ignore\_errors: true  
register: p2

- debug:  
var: p2

i’d like to compare:

- actionTo value with value from actionTo\_list
- and avoid printing “command not found” node without systemctl

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [November 17, 2018, 3:30am UTC](https://forum.ansible.com/t/dont-print-statement-if-command-not-found-and-compare-variable-value-with-list-of-values/29050/2 "2018-11-17T03:30:46Z")

</div>

I’d suggest not using the `shell:` module, instead use the `systemd:` module.

Then setup the `when:` clause with a third that only executes this play on OS versions that have `systemctl` (e.g. RHEL 7+, etc). You run a play right before it that registers the full path of `systemctl` found on the system (maybe use `shell: which systemctl`).

Something like this:

- name: manage services using systemctl  
tags: manage services using systemctl  
systemd:  
name: {{servicesList}}  
state: {{actionTodo}}  
with\_items:
- “{{servicesList}}”  
when:
- ansible\_os\_family == “RedHat” and ansible\_lsb.major\_release|int \>= 7
- actionTodo != “”
- servicesList != “”  
ignore\_errors: true  
register: p2

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [November 17, 2018, 3:44am UTC](https://forum.ansible.com/t/dont-print-statement-if-command-not-found-and-compare-variable-value-with-list-of-values/29050/3 "2018-11-17T03:44:42Z")

</div>

Correction to my previous email:  
You **COULD** run a play right before it that registers the full path of `systemctl` found on the system (maybe use `shell: which systemctl`), but that is not a stable method in some cases. I suggest using `systemd:` with the OS version check if needed.
