3

I am learning Ansible from this book and it contains this lab:

Write a playbook according to the following specifications:

  • The cron module must be used to restart your managed servers at 2 a.m. each weekday
  • After rebooting a message must be written to syslog with the text "CRON initiated reboot completed"
  • The default systemd target must be set to multi-target
  • The last task should use service facts to show the current version of the cron process

The last point is a bit confusing. The service_facts module only retrieves facts relating to the name,source,state and status of crond but not the version. How can this objective be achieved?

jstar100x
  • 33
  • 2

1 Answers1

1

This might be a mistake. The request looks suspicious (or ambiguous at least): 'use service facts to show the current version of the cron process'. There is no version among the attributes of the service facts, e.g. (running on Ubuntu)

    - service_facts:
    - debug:
        var: ansible_facts.services['cron.service']

gives

  ansible_facts.services['cron.service']:
    name: cron.service
    source: systemd
    state: running
    status: enabled

Moreover, 'version of process' looks weird. Perhaps they want 'PID of process'? In this case, use systemd, e.g.

    - systemd:
        name: "{{ ansible_facts.services['cron.service']['name'] }}"
      register: result
    - debug:
        var: result.status.ExecMainPID

gives

  result.status.ExecMainPID: '884'

(See also: shell> systemctl status cron.service)

The next option might be that they want 'version of cron binary'. In this case, use package_facts, e.g.

    - package_facts:
    - debug:
        var: ansible_facts.packages.cron.0.version

gives

  ansible_facts.packages.cron.0.version: 3.0pl1-136ubuntu1

See more sophisticated options in How to get which version of cron daemon is running. They might be automated in Ansible as well.

Vladimir Botka
  • 2,108
  • 10
  • 14
  • thanks, and yes I think its a mistake. The chapter for this exercise covers the systemd module so I presume it's that – jstar100x Jan 29 '22 at 23:03