0

I have used service_facts to a check service is running and enabled. In some of the server that specific package is not installed.
Now, how can I know that this particular package is not installed on that particular server by using service_facts module?

In an Ansible playbook it displaying the below error:

localhost | SUCCESS => {
    "ansible_facts": {
        "services": {
            "acpid.service": {
                "name": "acpid.service", 
                "source": "systemd", 
                "state": "running", 
                "status": "enabled"
            }, 
            "agent_installation.service": {
                "name": "agent_installation.service", 
                "source": "systemd", 
                "state": "stopped", 
                "status": "unknown"
            }, "changed": false, 
    "msg": "WARNING: Could not find status for all services. Sometimes this is due to insufficient privileges."
}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Suganthan Raj
  • 2,330
  • 6
  • 31
  • 42
  • I don't see any errors in your example output. What do you expect exactly ? – Zeitounator Oct 29 '21 at 16:37
  • This is the message i got from that particular server: ```"msg": "WARNING: Could not find status for all services. Sometimes this is due to insufficient privileges."``` – Suganthan Raj Oct 29 '21 at 16:39
  • Yes, it is a warning, not an error. So what do you expect exactly ? And what are your trying to check ? If you need a service state that is available only to root, you will need to escalate privilege. – Zeitounator Oct 29 '21 at 16:40
  • I need get service status of falcon and nessus using ansible_facts. this message is coming in which that falcon and nessus is not installed on that server. how to handle this in playbook. – Suganthan Raj Oct 29 '21 at 16:40
  • https://docs.ansible.com/ansible/latest/user_guide/become.html – Zeitounator Oct 29 '21 at 16:41
  • fatal: [localhost -> localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'falcon-sensor.service'"} ...ignoring – Suganthan Raj Oct 29 '21 at 16:52
  • 1
    That means either that you misspelled the service name (you should probably use `falcon-sensor` and not `falcon-sensor.service`) or that the service is not managed by _systemd_ and possibly by something else? – β.εηοιτ.βε Oct 29 '21 at 17:36

1 Answers1

1

A specific service can be available under service_facts only if it exists, is correctly registered. If you like to perform tasks on a service of which you don't know if it is there, Conditionals may work for you. In example

- name: Gathering Service Facts
  service_facts:

- name: Make sure {{ SERVICE }} is stopped and disabled
  systemd:
    name: {{ SERVICE }}
    state: stopped
    enabled: no
  when: ("{{ SERVICE }}" in services)

- name: Make sure {{ SERVICE }} is removed
  yum:
    name: {{ SERVICE }}
    state: absent

You could do something like

- name: Set facts
  set_fact:
    SERVICE: "postgresql-10.service" # for my working test environment

- name: Gathering service facts
  service_facts:

- name: Get facts
  debug:
    msg:
      - "{{ ansible_facts.services[SERVICE].status }}"
U880D
  • 8,601
  • 6
  • 24
  • 40