Improve operation and maintenance efficiency: Ansible systemd module operation skills, let you say goodbye to cumbersome command lines!

Improve operation and maintenance efficiency: Ansible systemd module operation skills, let you say goodbye to cumbersome command lines!

The systemd module in Ansible is a good helper to help you easily manage Linux system services. With this module, you can easily start, stop or restart services, and even set services to start automatically at boot time, which greatly simplifies the automated management of system services. In this article, we will introduce the functions and usage of the systemd module in detail, and show you how to manage your services efficiently through some practical cases.

Module Function Overview

The main functions of the systemd module are as follows:

  • Start the service
  • Stop service
  • Restart the service
  • Reload the service
  • Checking Service Status
  • Set the service to start automatically at boot
  • Disable service startup

Supported parameters

The following are commonly used parameters:

parameter

describe

name

Specify the name of the service to be managed

state

The expected state of the service. Optional values: started, stopped, restarted, reloaded

enabled

Whether to set the service to start automatically at boot, optional values: yes, no

daemon_reload

Whether to reload the systemd daemon configuration before the operation

masked

Whether to block the service and prevent it from starting

scope

Specifies the scope of a service, typically used to manage services in a container environment.

Actual case demonstration

The following are some practical examples to demonstrate how to use the systemd module to manage services.

1. Start the service and set it to start automatically

Scenario: Ensure that the nginx service is started and set to start automatically at boot.

Ad-hoc form:

 ansible webservers -m systemd -a \ "name='nginx' state='started' enabled='yes'"

Playbook format:

 - name:确保nginx服务已启动并开机自启hosts:webservers tasks: -name:启动nginx服务ansible.builtin.systemd: name:nginx state:started enabled:yes
  • state: started means the service must be in the running state.
  • enabled: yes means the service needs to be started automatically at boot.

After execution, if the nginx service on the target host is not started or is not set to start automatically at boot, Ansible will perform corresponding operations.

2. Stop the service and disable automatic startup

Scenario: Stop the apache2 service and disable its automatic startup.

  • Ad-hoc form:
 ansible webservers -m systemd -a \ "name='apache2' state='stopped' enabled='no'"
  • Playbook format:
 - name:停止apache2服务并禁用开机自启hosts:webservers tasks: -name:停止apache2服务ansible.builtin.systemd: name:apache2 state:stopped enabled:no
  • state: stopped Indicates that the service must be in the stopped state.
  • enabled: no means the service is not allowed to start automatically at boot.

3. Restart the service and reload the systemd configuration

Scenario: After changing the Docker configuration, you need to reload the systemd configuration and restart the service.

Ad-hoc form:

 ansible docker_hosts -m systemd -a \ "name='docker' state='restarted' daemon_reload='yes'"

Playbook format:

 - name:重启docker服务并重新加载systemd配置hosts:docker_hosts tasks: -name:重新加载systemd守护进程ansible.builtin.systemd: daemon_reload:yes -name:重启docker服务ansible.builtin.systemd: name:docker state:restarted

daemon_reload: yes Triggers a reload of the systemd daemon to apply new service configuration.

state: restarted Force restart of the service, whether or not it is already running.

4. Check service status and trigger processing

Scenario: Check the running status of the MySQL service. If it is not started, start the service.

  • Ad-hoc form:
 ansible database_servers -m systemd -a \ "name='mysql' state='started' "
  • Playbook format:
 - name:检查mysql服务状态并启动hosts:database_servers tasks: -name:检查mysql服务状态ansible.builtin.systemd: name:mysql state:started
  • state: started not only starts the service, but also checks the current service status. If it is already running, the operation will not be repeated.

5. Blocking Services

Scenario: To prevent some unnecessary services from starting, you can block services (such as bluetooth).

  • Ad-hoc form:
 ansible database_servers -m systemd -a \ "name='bluetooth' masked=yes "
  • Playbook format:
 - name:屏蔽bluetooth服务hosts:all tasks: -name:屏蔽服务以防止启动ansible.builtin.systemd: name:bluetooth masked:yes
  • masked: yes Masks the service, preventing it from starting, and even manual attempts to start it will fail.

Summarize

The systemd module of Ansible can greatly simplify the management of system services. Whether it is starting, stopping, setting automatic startup, or blocking services, this module can complete them efficiently. Combined with actual case operations, it not only improves automation capabilities, but also reduces human operational errors.

<<:  Three strategies to promote the comprehensive application of AI F5 continues to create a better digital world

>>: 

Recommend

How low-code platforms enable machine learning

【51CTO.com Quick Translation】 [[425497]] Low-code...

HostNamaste: $18/year-1.5GB/30GB/1.5TB Los Angeles & Dallas data centers

HostNamaste is a foreign hosting company founded ...

What can I do with 5G network?

Some people say that 4G is enough and there is no...

How 5G accelerates the development of the digital economy

Today, 5G construction is in full swing and gradu...

Industry insiders look at this: The history of 5G at the two sessions

[[327682]] A 5G+ holographic remote same-screen i...

"Disruption" or "Pie in the sky", what is the charm of OpenRAN?

OpenRAN (Open Radio Access Network) seems to be v...

Five technology trends everyone must prepare for in 2024

Machine intelligence, the blurring of the lines b...

NASA thinks the moon will soon have its own internet

It's been nearly 50 years since astronauts la...