15796

Ansible: disable service only if present

Is there any nice way to do disable and stop a service, but only if it's installed on server? Something like this:

- service: name={{ item }} enabled=no state=stopped only_if_present=yes with_items: - avahi-daemon - abrtd - abrt-ccpp

Note that "only_if_present" is a keyword that doesn't exist right now in Ansible, but I suppose my goal is obvious.

Answer1:

I don't know what is the package name in your case, but you can do something similar to this:

- shell: dpkg-query -W 'avahi' ignore_errors: True register: is_avahi when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' - shell: rpm -q 'avahi' ignore_errors: True register: is__avahi when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' - service: name=avahi-daemon enabled=no state=stopped when: is_avahi|failed

Update: I have added conditions so that the playbook works when you have multiple different distros, you might need to adapt it to fit your requirements.

Recommend

  • Ansible not allowing environment at playbook level
  • Celery Production Graceful Restart
  • Is it possible to run a playbook in “pull mode”?
  • Updating numerical types using NoRM and other tools changes numerical type metadata on MongoDb
  • windows “.lnk” shortcuts and batches don't mix
  • Mongolab connection error
  • Mongo server accepts credentials from shell, but not from Java/Scala interface
  • Bash while read LINE extremely slow compared to cat, why?
  • Write a function that calculate the sum of integers in a list in Erlang
  • How do I clear the screen in C? [duplicate]
  • Python: Adding to dict of one object in a list changes all dicts of every other object in the list
  • Delete file in internal memory from Android device?
  • Manage different base layouts in Angular2
  • Copy folder content with Ansible
  • Getting directory of input file (Applescript)
  • Any command in mysql equivalent to Oracle's autotrace for performance turning
  • Polymer build not to create bundled and unbundled folder
  • How to grep for a specific pattern and print everything above and below that pattern until you reach
  • JPA CDI Injecting DAO into an Entity
  • How to debug Shell command after customization
  • Mongodb update() vs. findAndModify() performace
  • Vigenere cipher not working
  • user data scripts fails without giving reason
  • Swift Initialization Rule Confusion
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • How can I get the full list of running processes on a Mac from a python app
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Caching attributes in superclass
  • Xcode 4 NSLog Macro link in Xcode 3
  • Spark fat jar to run multiple versions on YARN
  • ActionScript 2 vs ActionScript 3 performance
  • How to pass list parameters for each object using Spring MVC?
  • json Serialization in asp
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • JaxB to read class hierarchy