0

I'm setting up a new playbook, and want to support both service status and systemctl status. I want to monitor a service called Squid on Ubuntu 14.04 that has only service and 16.04 that has both service and systemctl.

I had tried using How to get service status by Ansible? But it gives me an answer only for service or systemctl.

- hosts: servers
  gather_facts: true
  tasks:
    - name: Run the specified shell command
      shell: 'service squid status'
      register: result

    - name: Display the outcome
      debug:
        msg: '{{ result }}'

I'm trying to get both service status from service and systemctl in the same playbook run. But the actual result from that .yml file is only explicit for Systemctl status or service status

Dor Shamay
  • 345
  • 1
  • 4
  • 21
  • 1
    On instances where systemctl has been implemented, running a service command redirects to systemctl. So it's not an ansible issue in as much as the host. – WarrenG Apr 15 '19 at 12:27
  • 1
    in addition to the above comment you should use the service module of ansible and not the shell. Also command mnodule should be used instead of shell in case you don't want to use the service module at all – error404 Apr 15 '19 at 12:33
  • I would use the service module but it answers the question? – Dor Shamay Apr 15 '19 at 12:58
  • If you are particularly looking for status irrespective of the current state you should using command module. Register it's output and debug it. service module has limited options. But before using service module your service should be loaded in systemctl or in /etc/init.d. Once it has been loaded the system will take care of the status – error404 Apr 15 '19 at 13:25
  • The service does load on systemctl or init.d but some of the machines call the service squid and some of the older distro's call it squid3, any advice? – Dor Shamay Apr 16 '19 at 06:29
  • 1
    The solution for this is to just do `command` or `shell` and execute the command that you want to execute. Other than that, create a `when` condition that checks the ansible_facts variables and if it's Ubuntu 16 do X, if it's Ubuntu 14 do Y (In the when condition) – Shlomi Bazel Apr 16 '19 at 08:49
  • I'll try to search online for that to do a `when` condition. – Dor Shamay Apr 17 '19 at 13:28

0 Answers0