第 27 章 Managing systemd units by using RHEL system roles
By using the systemd RHEL system role, you can automate certain systemd-related tasks and perform them remotely.
You can use the systemd role for the following actions:
- Manage services
- Deploy units
- Deploy drop-in files
You can automate and remotely manage systemd units, such as starting or enabling services, by using the systemd RHEL system role.
Prerequisites
- You have prepared the control node and the managed nodes.
- You are logged in to the control node as a user who can run playbooks on the managed nodes.
-
The account you use to connect to the managed nodes has
sudopermissions for these nodes.
Procedure
Create a playbook file, for example,
~/playbook.yml, with the following content. Use only the variables depending on what actions you want to perform.--- - name: Managing systemd services hosts: managed-node-01.example.com tasks: - name: Perform action on systemd units ansible.builtin.include_role: name: redhat.rhel_system_roles.systemd vars: systemd_started_units: - <systemd_unit_1>.service systemd_stopped_units: - <systemd_unit_2>.service systemd_restarted_units: - <systemd_unit_3>.service systemd_reloaded_units: - <systemd_unit_4>.service systemd_enabled_units: - <systemd_unit_5>.service systemd_disabled_units: - <systemd_unit_6>.service systemd_masked_units: - <systemd_unit_7>.service systemd_unmasked_units: - <systemd_unit_8>.serviceFor details about all variables used in the playbook, see the
/usr/share/ansible/roles/rhel-system-roles.systemd/README.mdfile on the control node.Validate the playbook syntax:
$ ansible-playbook --syntax-check ~/playbook.ymlNote that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
$ ansible-playbook ~/playbook.yml