Este contenido no está disponible en el idioma seleccionado.
Chapter 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
27.1. Managing services by using the systemd RHEL system role Copiar enlaceEnlace copiado en el portapapeles!
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 on them.
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.Copy to Clipboard Copied! Toggle word wrap Toggle overflow For 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.yml
$ ansible-playbook --syntax-check ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
ansible-playbook ~/playbook.yml
$ ansible-playbook ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
27.2. Deploying systemd drop-in files by using the systemd RHEL system role Copiar enlaceEnlace copiado en el portapapeles!
Systemd applies drop-in files on top of setting it reads for a unit from other locations. Therefore, you can modify unit settings with drop-in files without changing the original unit file. By using the systemd RHEL system role, you can automate the process of deploying drop-in files.
The role uses the hard-coded file name 99-override.conf to store drop-in files in /etc/systemd/system/<name>._<unit_type>/. Note that it overrides existing files with this name in the destination directory.
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 on them.
Procedure
Create a Jinja2 template with the systemd drop-in file contents. For example, create the
~/sshd.service.conf.j2file with the following content:{{ ansible_managed | comment }} [Unit] After= After=network.target sshd-keygen.target network-online.target{{ ansible_managed | comment }} [Unit] After= After=network.target sshd-keygen.target network-online.targetCopy to Clipboard Copied! Toggle word wrap Toggle overflow This drop-in file specifies the same units in the
Aftersetting as the original/usr/lib/systemd/system/sshd.servicefile and, additionally,network-online.target. With this extra target,sshdstarts after the network interfaces are actived and have IP addresses assigned. This ensures thatsshdcan bind to all IP addresses.Use the
<name>.<unit_type>.conf.j2convention for the file name. For example, to add a drop-in for thesshd.serviceunit, you must name the filesshd.service.conf.j2. Place the file in the same directory as the playbook.Create a playbook file, for example,
~/playbook.yml, with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The settings specified in the example playbook include the following:
systemd_dropins: <list_of_files>- Specifies the names of the drop-in files to deploy in YAML list format.
For 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.yml
$ ansible-playbook --syntax-check ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
ansible-playbook ~/playbook.yml
$ ansible-playbook ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the role placed the drop-in file in the correct location:
ansible managed-node-01.example.com -m command -a 'ls /etc/systemd/system/sshd.service.d/'
# ansible managed-node-01.example.com -m command -a 'ls /etc/systemd/system/sshd.service.d/' 99-override.confCopy to Clipboard Copied! Toggle word wrap Toggle overflow
27.3. Deploying systemd system units by using the systemd RHEL system role Copiar enlaceEnlace copiado en el portapapeles!
You can create unit files for custom applications, and systemd reads them from the /etc/systemd/system/ directory. By using the systemd RHEL system role, you can automate the deployment of custom unit files.
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 on them.
Procedure
Create a Jinja2 template with the custom systemd unit file contents. For example, create the
~/example.service.j2file with the contents for your service:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
<name>.<unit_type>.j2convention for the file name. For example, to create theexample.serviceunit, you must name the fileexample.service.j2. Place the file in the same directory as the playbook.Create a playbook file, for example,
~/playbook.yml, with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow For 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.yml
$ ansible-playbook --syntax-check ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
ansible-playbook ~/playbook.yml
$ ansible-playbook ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the service is enabled and started:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow