I’m using Ansible to manage a small fleet of Raspberry Pis. I’d been using the copy module to set a value in /sys:
- name: Enable compressed swap now (with zsmalloc)
become: true
ansible.builtin.copy:
content: zsmalloc
dest: /sys/module/zswap/parameters/zpool
unsafe_writes: true
But that always reports that the file changed, even if it already had that value. Today I got the lineinfile module to update the value. This only says the value changed when it actually did:
- name: Enable compressed swap now (with zsmalloc)
become: true
ansible.builtin.lineinfile:
path: /sys/module/zswap/parameters/zpool
regexp: "^(?!zsmalloc).*$"
line: zsmalloc
unsafe_writes: true
Since these “files” only have one line, this uses regexp to see if that line already matches the expected value. If so, it moves on. If not, it writes the new value.
Note: unsafe_writes: true is there because you can’t write arbitrary filenames into /sys and then mv them into place. You have to write directly to the target “file”.