Chapter 6. Configuring kernel parameters permanently by using the kernel_settings RHEL System Role
As an experienced user with good knowledge of Red Hat Ansible, you can use the kernel_settings
role to configure kernel parameters on multiple clients at once. This solution:
- Provides a friendly interface with efficient input setting.
- Keeps all intended kernel parameters in one place.
After you run the kernel_settings
role from the control machine, the kernel parameters are applied to the managed systems immediately and persist across reboots.
Note that RHEL System Role delivered over RHEL channels are available to RHEL customers as an RPM package in the default AppStream repository. RHEL System Role are also available as a collection to customers with Ansible subscriptions over Ansible Automation Hub.
6.1. Introduction to the kernel_settings role
RHEL System Roles is a set of roles that provide a consistent configuration interface to remotely manage multiple systems.
RHEL System Roles were introduced for automated configurations of the kernel using the kernel_settings
System Role. The rhel-system-roles
package contains this system role, and also the reference documentation.
To apply the kernel parameters on one or more systems in an automated fashion, use the kernel_settings
role with one or more of its role variables of your choice in a playbook. A playbook is a list of one or more plays that are human-readable, and are written in the YAML format.
You can use an inventory file to define a set of systems that you want Ansible to configure according to the playbook.
With the kernel_settings
role you can configure:
-
The kernel parameters using the
kernel_settings_sysctl
role variable -
Various kernel subsystems, hardware devices, and device drivers using the
kernel_settings_sysfs
role variable -
The CPU affinity for the
systemd
service manager and processes it forks using thekernel_settings_systemd_cpu_affinity
role variable -
The kernel memory subsystem transparent hugepages using the
kernel_settings_transparent_hugepages
andkernel_settings_transparent_hugepages_defrag
role variables
Additional resources
-
README.md
andREADME.html
files in the/usr/share/doc/rhel-system-roles/kernel_settings/
directory - Working with playbooks
- How to build your inventory
6.2. Applying selected kernel parameters using the kernel_settings
role
Follow these steps to prepare and apply an Ansible playbook to remotely configure kernel parameters with persisting effect on multiple managed operating systems.
Prerequisites
-
You have
root
permissions. -
Entitled by your RHEL subscription, you installed the
ansible-core
andrhel-system-roles
packages on the control machine. - An inventory of managed hosts is present on the control machine and Ansible is able to connect to them.
Procedure
Optionally, review the
inventory
file for illustration purposes:# cat /home/jdoe/<ansible_project_name>/inventory [testingservers] pdoe@192.168.122.98 fdoe@192.168.122.226 [db-servers] db1.example.com db2.example.com [webservers] web1.example.com web2.example.com 192.0.2.42
The file defines the
[testingservers]
group and other groups. It allows you to run Ansible more effectively against a specific set of systems.Create a configuration file to set defaults and privilege escalation for Ansible operations.
Create a new YAML file and open it in a text editor, for example:
# vi /home/jdoe/<ansible_project_name>/ansible.cfg
Insert the following content into the file:
[defaults] inventory = ./inventory [privilege_escalation] become = true become_method = sudo become_user = root become_ask_pass = true
The
[defaults]
section specifies a path to the inventory file of managed hosts. The[privilege_escalation]
section defines that user privileges be shifted toroot
on the specified managed hosts. This is necessary for successful configuration of kernel parameters. When Ansible playbook is run, you will be prompted for user password. The user automatically switches toroot
by means ofsudo
after connecting to a managed host.
Create an Ansible playbook that uses the
kernel_settings
role.Create a new YAML file and open it in a text editor, for example:
# vi /home/jdoe/<ansible_project_name>/kernel-roles.yml
This file represents a playbook and usually contains an ordered list of tasks, also called plays, that are run against specific managed hosts selected from your
inventory
file.Insert the following content into the file:
--- - hosts: testingservers name: "Configure kernel settings" roles: - rhel-system-roles.kernel_settings vars: kernel_settings_sysctl: - name: fs.file-max value: 400000 - name: kernel.threads-max value: 65536 kernel_settings_sysfs: - name: /sys/class/net/lo/mtu value: 65000 kernel_settings_transparent_hugepages: madvise
The
name
key is optional. It associates an arbitrary string with the play as a label and identifies what the play is for. Thehosts
key in the play specifies the hosts against which the play is run. The value or values for this key can be provided as individual names of managed hosts or as groups of hosts as defined in theinventory
file.The
vars
section represents a list of variables containing selected kernel parameter names and values to which they have to be set.The
roles
key specifies what system role is going to configure the parameters and values mentioned in thevars
section.NoteYou can modify the kernel parameters and their values in the playbook to fit your needs.
Optionally, verify that the syntax in your play is correct.
# ansible-playbook --syntax-check kernel-roles.yml playbook: kernel-roles.yml
This example shows the successful verification of a playbook.
Execute your playbook.
# ansible-playbook kernel-roles.yml ... BECOME password: PLAY [Configure kernel settings] ********************************************************************************** PLAY RECAP ******************************************************************************************************** fdoe@192.168.122.226 : ok=10 changed=4 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0 pdoe@192.168.122.98 : ok=10 changed=4 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0
Before Ansible runs your playbook, you are going to be prompted for your password and so that a user on managed hosts can be switched to
root
, which is necessary for configuring kernel parameters.The recap section shows that the play finished successfully (
failed=0
) for all managed hosts, and that 4 kernel parameters have been applied (changed=4
).- Restart your managed hosts and check the affected kernel parameters to verify that the changes have been applied and persist across reboots.
Additional resources
- Preparing a control node and managed nodes to use RHEL System Roles
-
README.html
andREADME.md
files in the/usr/share/doc/rhel-system-roles/kernel_settings/
directory - Build Your Inventory
- Configuring Ansible
- Working With Playbooks
- Using Variables
- Roles