What you'll learn
- How to set the sysctl kernel parameters with Ansible?
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
- Set Kernel Parameter
How to set the sysctl kernel parameters with Ansible?
**`ansible.posix.sysctl`** is the Ansible module for configuring Linux kernel parameters persistently. It modifies `/etc/sysctl.conf` entries to tune networking (TCP/IP stack), memory (swap, cache), and security settings—with changes that survive system reboots and apply immediately when `sysctl_set: true` and `reload: true` are set.
The full name is `ansible.posix.sysctl`, part of the "ansible.posix" collection for POSIX-compliant systems. The module's purpose is to manage `sysctl.conf` entries idempotently—ensuring your kernel parameters are always at the desired state, whether you're tuning for performance, security, or container orchestration (Docker/Kubernetes).
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] ********************************************