Last active
October 19, 2023 20:53
-
-
Save tyrells/0a79681de339237cb04c to your computer and use it in GitHub Desktop.
Ansible task to stop service even if it doesn't exist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This task will stop and disable a service without failing if the service does not exist. | |
# Requires Ansible 1.4 or newer. | |
# Update Dec 2016: Have rewritten this for the latest version of ansible and put conditions for both Ubuntu and CentOS | |
- name: "disable unused services" | |
service: name={{item}} state=stopped enabled=no | |
register: command_result | |
failed_when: "unused_disable|failed and ('find' not in unused_disable.msg and 'found' not in unused_disable.msg)" | |
with_items: | |
- httpd | |
- sendmail |
A combination of comments here got it working for me:
---
- name: Disable Metrics Utilites
hosts: all
tasks:
- name: Stop and disable unused services
service: name={{item}} state=stopped enabled=no
register: unused_disable
failed_when: "unused_disable is failed and ('find' not in unused_disable.msg and 'found' not in unused_disable.msg)"
with_items:
- metricbeat
- auditbeat
- telegraf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just for info - with Ansible v2.9 and later, you cannot use a jinja test as a filter. Consequently, to get this to work, replace
unused_disable|failed
with:unused_disable is failed