When editing configuration files, such as /etc/sysctl.conf for example, it is often useful to do the update in an idempotent way, meaning that if the script is executed multiple times, you don't end up with multiple entries for the configuration change you made.
As a real-world instance where I encountered this, I need to edit the above file in multi-stage ansible playbook. But the problem is that if the later stage fails, the playbook will need to be run again, which means that the update command may run multiple times, causing duplication if the update is not idempotent.
So the question is how can you update such configuration files in an idempotent way?
An example of a non-idempotent update would be:
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
or in ansible:
- name: Set swappiness setting
shell: echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
Ideally if the value of the variable is initially set, it should be replaced with new value.
/etc/paths.d/,/etc/network/interfaces.d/,/etc/apt/sources.list.d/,/etc/netplan/, and so on. Unfortunately, this realization is still not universal.