I'm attempting to update /boot/firmware/cmdline.txt
on a few Raspberry Pis to enable cgroup
support. Seemed a good fit for ansible.builtin.lineinfile
.
The existing contents of /boot/firmware/cmdline.txt
are a single line of key-value pairs
console=serial0,115200 dwc_otg.lpm_enable=0 console=tty1 root=LABEL=writable rootfstype=ext4 rootwait fixrtc quiet splash
I want the following key-value pairs to be appended to any existing contents of that file that might exist.
cgroup_enable=cpuset
cgroup_enable=memory
cgroup_memory=1
swapaccount=1
In the end, I want the file to look like
console=serial0,115200 dwc_otg.lpm_enable=0 console=tty1 root=LABEL=writable rootfstype=ext4 rootwait fixrtc quiet splash cgroup_enable=cpuset cgroup_enable=memory cgroup_memory=1 swapaccount=1
Here's the task I created:
- name: "Modify /boot/firmware/cmdline.txt to enable cGroup support if not already enabled"
ansible.builtin.lineinfile:
path: /boot/firmware/cmdline.txt
regexp: '(.*) ((cgroup_enable=cpuset|cgroup_enable=memory|cgroup_memory=1|swapaccount=1)( {0,}|\n)){4}(.*)'
line: '$1 cgroup_enable=cpuset cgroup_enable=memory cgroup_memory=1 swapaccount=1 $5'
state: present
backrefs: true
backup: true
owner: root
group: root
mode: '0755'
Unfortunately, it doesn't seem to update the file and I'm struggling to understand why. To clarify, the task executes fine but indicates that the file already matches the regexp with the following output:
TASK [common : Modify /boot/firmware/cmdline.txt to enable cGroup support if not already enabled] **************************
ok: [kube02.local]
ok: [kube03.local]
ok: [kube01.local]
As far as I can tell, it does not and I've tested in a few ways. Increasing verbosity of this doesn't help me figure this out, unfortunately.
I did find this post's answer which has a more robust solution, but I would love to understand why my solution doesn't work.