ansiblesysctl

Setting multiple values in sysctl with Ansible


I have a playbook with several tasks setting values to sysctl. Instead of having a task for each setting, how can I set all the values with one task, using the sysctl module?

Playbook snippet:

- name: Set tcp_keepalive_probes in sysctl
  become: yes
  sysctl:
    name: net.ipv4.tcp_keepalive_probes
    value: 3
    state: present
    reload: yes

- name: Set tcp_keepalive_intvl in sysctl
  become: yes
  sysctl:
    name: net.ipv4.tcp_keepalive_intvl
    value: 10
    state: present
    reload: yes

- name: Set rmem_default in sysctl
  become: yes
  sysctl:
    name: net.core.rmem_default
    value: 16777216
    state: present
    reload: yes

Solution

  • define all the variables in a var file:

    e.g.

    sysctl:
      - name: test
        value: test
    

    ... ...

    playbook:

    - hosts: "{{ }}"
      tasks: 
        - name: update sysctl param
          sysctl:
            name: "{{ item.name }}"
            value: "{{ item.value }}"
            state: present
            reload: yes
          with_items:
            - "{{ hosts }}"