Ansible sysctl Module: Set Linux Kernel Parameters (Persistent) — Video Tutorial
Configure Linux kernel parameters with Ansible ansible.posix.sysctl module. Set net, vm, fs parameters persistently with validation and rollback examples.
Watch Video
Watch "Ansible sysctl Module: Set Linux Kernel Parameters (Persistent)" on YouTube
What You'll Learn
- How to set the sysctl kernel parameters with Ansible?
- Ansible set sysctl kernel parameters
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to set the sysctl kernel parameters with Ansible?
I'm going to show you a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible set sysctl kernel parameters
- ansible.posix.sysctl
- Manage entries in sysctl.conf
Today we're talking about the Ansible module sysctl.
The full name is `ansible.posix.sysctl`, which means that is part of the collection of modules "ansible.posix" to interact with POSIX platforms.
The purpose of the module is to manage entries in the `sysctl.conf` file.
Parameters
- name string (key) - Parameter name
- value string - Parameter value
- reload boolean - yes/no
- state string - present/absent
- sysctl_file string - "/etc/sysctl.conf"
- sysctl_set string - no/yes - sysctl -w
- ignoreerrors boolean - no/yes
Let me summarize the parameters of sysctl module.
The only required is "name", where you specify the parameter name to access or edit.
The parameter "value" sets the value of the sysctl parameter.
The parameter "reload", default to yes, reload the configuration file if any changes occur.
The parameter "state" sets the presence or absence of the parameter in the sysctl file.
The parameter "sysctl_file" allows specifying the configuration file for sysctl, default to "/etc/sysctl.conf".
The parameter "sysctl_set" allows you to configure a parameter permanently, that survives after reboot.
The parameter "ignoreerrors" allow you to ignore errors about unknown keys, default to "no".
Links
https://docs.ansible.com/ansible/latest/collections/ansible/posix/sysctl_module.html
## Playbook
Ansible set sysctl kernel parameters.
code
```yaml
---
- name: sysctl module Playbook
hosts: all
become: true
vars:
sysctl_name: "vm.swappiness"
sysctl_value: "5"
tasks:
- name: set sysctl
ansible.posix.sysctl:
name: "{{ sysctl_name }}"
value: "{{ sysctl_value }}"
state: present
sysctl_set: true
reload: true
```
execution
```bash
$ ansible-playbook -i virtualmachines/demo/inventory sysctl/sysctl.yml
PLAY [sysctl module Playbook] *************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [ansible.posix.sysctl] ***********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
idempotency
```bash
$ ansible-playbook -i virtualmachines/demo/inventory sysctl/sysctl.yml
PLAY [sysctl module Playbook] *************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [ansible.posix.sysct
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 6 min
- Category: troubleshooting
Read the full written article: Ansible sysctl Module: Set Linux Kernel Parameters (Persistent)
Topics Covered
Related Video Tutorials
- Configuring Kernel Parameters in RedHat-like Linux Systems with Ansible System Role — Learn how to configure kernel parameters in RedHat-like Linux systems using the Ansible System Role. Follow our live Playbook example for efficient system management.
- Ansible modprobe: Load & Unload Linux Kernel Modules (Guide) — How to load and unload Linux kernel modules with Ansible modprobe module. Manage drivers, configure module parameters, and persist across reboots with examples.
- Ansible code in RHSB-2021-009 Log4Shell - Remote Code Execution - log4j (CVE-2021-44228) — Learn how my Ansible Playbook was featured in Red Hat Security Bulletin RHSB-2021-009 to address the Log4Shell vulnerability (CVE-2021-44228). Discover the playbook and its role in mitigating security risks.
- Automate CIS Benchmark Hardening for RHEL 9 with Ansible — Discover how to automate CIS Benchmark hardening for RHEL 9 systems using Ansible. Simplify security compliance with the "ansible-lockdown" project for efficient, effective protection.
- Strengthening Security: Automating CIS Benchmark Hardening for RHEL 9 with Ansible — Automate CIS Benchmark hardening for RHEL 9 using Ansible with the Ansible Lockdown roles, ensuring robust security configurations across systems.
- Ansible SELinux: Manage Modes, Booleans & Contexts (Complete Guide) — How to automate the enabling or disabling of SELinux Permissive policy per single process or domain keeping the whole system under enforcing policy and make it persistent after a reboot on Linux with Ansible.