+
Skip to content

sysctl module is not idempotent when modifying custom sysctl files on Ubuntu #680

@gdrosos

Description

@gdrosos
SUMMARY

When using the ansible.posix.sysctl module to set a sysctl key to a value that already exists in a custom sysctl file, the module incorrectly reports changed: true on subsequent runs on Ubuntu (22.04 and 24.04), even though the file is already correctly configured. This breaks idempotence.

The first run correctly returns changed: true (since the key is being set), but all following runs should return changed: false if no changes are necessary. On Ubuntu, they incorrectly keep returning changed: true.

Interestingly, the same task behaves correctly on other operating systems like, Fedora, and Arch, where subsequent runs are idempotent and return changed: false.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

sysctl

ANSIBLE VERSION
ansible [core 2.18.7]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.13/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.13.5 (main, Jun 11 2025, 22:06:31) [GCC] (/usr/bin/python3.13)
  jinja version = 3.1.6
  libyaml = True
COLLECTION VERSION
Collection    Version
------------- -------
ansible.posix 1.6.2  
CONFIGURATION

OS / ENVIRONMENT

Ubuntu 22.04, Ubuntu 24.04

STEPS TO REPRODUCE
- name: Reproduce sysctl changed bug on Ubuntu
  hosts: localhost
  gather_facts: false
  become: true
  tasks:
    - name: Set existing identical value
      ansible.posix.sysctl:
        name: vm.swappiness
        value: "10"
        sysctl_file: /tmp/idempotent1.conf
      register: test_result

    - name: Set existing identical value (idemptency)
      ansible.posix.sysctl:
        name: vm.swappiness
        value: "10"
        systl_file: /tmp/idempotent1.conf
                                            
EXPECTED RESULTS

We would expect the second time we execute module to get an ok status

ACTUAL RESULTS
TASK [Set existing identical value] **********************************************************************************************************************************************************************************
task path: /tmp/e.yml:6
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1756124028.325896-3885-177548708992098 `" && echo ansible-tmp-1756124028.325896-3885-177548708992098="` echo /root/.ansible/tmp/ansible-tmp-1756124028.325896-3885-177548708992098 `" ) && sleep 0'
Using module file /opt/python3.12/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/modules/sysctl.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-3882c_clc1fe/tmptnzzu__6 TO /root/.ansible/tmp/ansible-tmp-1756124028.325896-3885-177548708992098/AnsiballZ_sysctl.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+rwx /root/.ansible/tmp/ansible-tmp-1756124028.325896-3885-177548708992098/ /root/.ansible/tmp/ansible-tmp-1756124028.325896-3885-177548708992098/AnsiballZ_sysctl.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/opt/python3.12/bin/python3.12 /root/.ansible/tmp/ansible-tmp-1756124028.325896-3885-177548708992098/AnsiballZ_sysctl.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1756124028.325896-3885-177548708992098/ > /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "ignoreerrors": false,
            "name": "vm.swappiness",
            "reload": true,
            "state": "present",
            "sysctl_file": "/tmp/idempotent.conf",
            "sysctl_set": false,
            "value": "10"
        }
    }
}

TASK [Set existing identical value (idemptency)] *********************************************************************************************************************************************************************
task path: /tmp/e.yml:13
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1756124029.7301872-3912-50620302340472 `" && echo ansible-tmp-1756124029.7301872-3912-50620302340472="` echo /root/.ansible/tmp/ansible-tmp-1756124029.7301872-3912-50620302340472 `" ) && sleep 0'
Using module file /opt/python3.12/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/modules/sysctl.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-3882c_clc1fe/tmpgeot1im1 TO /root/.ansible/tmp/ansible-tmp-1756124029.7301872-3912-50620302340472/AnsiballZ_sysctl.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+rwx /root/.ansible/tmp/ansible-tmp-1756124029.7301872-3912-50620302340472/ /root/.ansible/tmp/ansible-tmp-1756124029.7301872-3912-50620302340472/AnsiballZ_sysctl.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/opt/python3.12/bin/python3.12 /root/.ansible/tmp/ansible-tmp-1756124029.7301872-3912-50620302340472/AnsiballZ_sysctl.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1756124029.7301872-3912-50620302340472/ > /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "ignoreerrors": false,
            "name": "vm.swappiness",
            "reload": true,
            "state": "present",
            "sysctl_file": "/tmp/idempotent.conf",
            "sysctl_set": false,
            "value": "10"
        }
    }
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载