Ansible Pilot

Stop and disable services on boot on remote hosts - Ansible module service_facts, service

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to list the available Linux services and automate the stop and disable service on boot process with Ansible playbook. Included code and demo with chronyd.service NTP server on a RedHat Enterprise Linux 8.

How to stop and disable services on boot on Linux remote hosts with Ansible?

I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible enable services on boot on remote hosts

Today we’re talking about Ansible modules service_facts and service. First, you need to acquire the information of the services on the target machine. This task is performed by the Ansible module service_facts. You can’t enable a service that doesn’t exist, can you? The effective actions are performed by the Ansible module service. The full name is ansible.builtin.service which means that both these modules are part of the collection of modules “builtin” with Ansible and shipped with it. This module is pretty stable and out for years and its purpose is to manage services on remote hosts. For Windows targets, use the ansible.windows.win_service module instead.

Parameters

The parameter list is pretty wide but I’ll summarize the most useful. The only required parameter is “name” that specifies the name of the service. At least one between the “state” and “enabled” parameters is mandatory. The “state” parameter defines the action that we are going to take. It has four alternative options: “started” and “stopped” options allow you to run or stop the service. “restarted” is a combination of stop and start - you could also customize the number of seconds between using the “sleep” parameter The “reloaded” option is useful if the service needs to reload the configuration file. The “enable” parameter allows you to decide if the service should start on boot or not. The “arguments or args” parameter allows you to specify some additional arguments provided on the command line.

demo

Stop and disable services on boot on Linux remote hosts with Ansible Playbook. Included code and demo with chronyd.service NTP server on a RedHat Enterprise Linux 8.

code

---
- name: service module demo
  hosts: all
  become: true
  vars:
    disable_services:
      - "chronyd.service"
  tasks:
    - name: populate service facts
      ansible.builtin.service_facts:

    - name: disable services
      ansible.builtin.service:
        name: "{{ item }}"
        enabled: false
        state: stopped
      when: "item in services"
      with_items: '{{ disable_services }}'

execution

$ ansible-playbook -i virtualmachines/demo/inventory services/service_stop_disable.yml
PLAY [service module demo] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [populate service facts] *********************************************************************
ok: [demo.example.com]
TASK [disable services] ***************************************************************************
changed: [demo.example.com] => (item=chronyd.service)
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

before execution

$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] devops]# cat /etc/redhat-release 
Red Hat Enterprise Linux release 8.4 (Ootpa)
[[email protected] devops]# rpm -qa | grep chrony
chrony-3.5-2.el8.x86_64
[[email protected] devops]# systemctl status chronyd.service
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-11-29 11:31:32 UTC; 1 day 21h ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 825 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCE>
  Process: 811 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 820 (chronyd)
    Tasks: 1 (limit: 4943)
   Memory: 1.3M
   CGroup: /system.slice/chronyd.service
           └─820 /usr/sbin/chronyd
Nov 30 13:59:47 demo.example.com chronyd[820]: Can\'t synchronise: no majority
Nov 30 14:20:47 demo.example.com chronyd[820]: Selected source 147.251.48.140
Nov 30 14:33:43 demo.example.com chronyd[820]: Selected source 89.221.218.101
Nov 30 14:39:26 demo.example.com chronyd[820]: Source 108.61.164.200 replaced with 5.79.75.37
Nov 30 15:01:14 demo.example.com chronyd[820]: Selected source 147.251.48.140
Dec 01 07:18:26 demo.example.com chronyd[820]: Forward time jump detected!
Dec 01 07:18:26 demo.example.com chronyd[820]: Can\'t synchronise: no selectable sources
Dec 01 07:20:35 demo.example.com chronyd[820]: Selected source 147.251.48.140
Dec 01 07:23:49 demo.example.com chronyd[820]: Selected source 89.234.64.77
Dec 01 07:33:31 demo.example.com chronyd[820]: Selected source 89.221.218.101
[[email protected] devops]#

after execution

$ ssh [email protected]
Last login: Wed Dec  1 08:39:40 2021 from 192.168.0.101
[[email protected] ~]$ sudo su
[[email protected] devops]# systemctl status chronyd.service
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)
Nov 30 15:01:14 demo.example.com chronyd[820]: Selected source 147.251.48.140
Dec 01 07:18:26 demo.example.com chronyd[820]: Forward time jump detected!
Dec 01 07:18:26 demo.example.com chronyd[820]: Can\'t synchronise: no selectable sources
Dec 01 07:20:35 demo.example.com chronyd[820]: Selected source 147.251.48.140
Dec 01 07:23:49 demo.example.com chronyd[820]: Selected source 89.234.64.77
Dec 01 07:33:31 demo.example.com chronyd[820]: Selected source 89.221.218.101
Dec 01 08:39:23 demo.example.com systemd[1]: Stopping NTP client/server...
Dec 01 08:39:23 demo.example.com chronyd[820]: chronyd exiting
Dec 01 08:39:23 demo.example.com systemd[1]: chronyd.service: Succeeded.
Dec 01 08:39:23 demo.example.com systemd[1]: Stopped NTP client/server.
[[email protected] devops]# reboot
Connection to demo.example.com closed by remote host.
Connection to demo.example.com closed.
ansible-pilot $ ssh [email protected]
Last login: Wed Dec  1 08:40:41 2021 from 192.168.0.101
[[email protected] ~]$ sudo su
[[email protected] devops]# uptime
 08:41:08 up 0 min,  1 user,  load average: 0.28, 0.06, 0.02
[[email protected] devops]# systemctl status chronyd.service
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)
[[email protected] devops]#

code with ❤️ in GitHub

Recap

Now you know how to stop and disable services on boot on Linux remote hosts with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Access the Complete Book and Learn Quick Ansible by 200+ Examples

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

December 1, 2021

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress