From 2d5c5d0c7864cdb90aed05b4332fc2d0f2dcf932 Mon Sep 17 00:00:00 2001 From: Chris Coley <chris@codingallnight.com> Date: Wed, 7 Mar 2018 11:43:05 -0800 Subject: [PATCH] Rewriting the dtoverlay code to guarantee ordering. Fixes #1 --- tasks/main.yaml | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/tasks/main.yaml b/tasks/main.yaml index f53b4be..b775c93 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -2,7 +2,7 @@ # We need to make sure dtoverlay is set to any non-empty value - name: Check if 'dtoverlay' is already set - shell: grep "^dtoverlay=." /boot/config.txt + shell: grep -E "^dtoverlay *=.+" /boot/config.txt register: check_dtoverlay failed_when: check_dtoverlay.rc == 2 changed_when: false @@ -12,21 +12,28 @@ lineinfile: dest: /boot/config.txt line: dtoverlay=i2c-rtc,ds3231 - when: check_dtoverlay.rc == 1 + when: (check_dtoverlay.rc is defined) and (check_dtoverlay.rc == 1) -# If dtoverlay is set, but doesn't contain i2c-rtc, prepend it to the beginning -- name: "Configure the 'dtoverlay' for i2c-rtc" - replace: - dest: /boot/config.txt - regexp: '^dtoverlay=((?!.*i2c-rtc).+)' - replace: 'dtoverlay=i2c-rtc,\1' +# Get the existing dtoverlay value if it is set +- name: Get current 'dtoverlay' value + shell: tac /boot/config.txt \ + | grep -E -m 1 '^dtoverlay *=.+' \ + | tr -d '[:space:]' \ + | cut -d = -f 2 \ + | sed -r "s/i2c-rtc//g; s/ds3231//g; s/^,+//g; s/,+$//g" \ + | tr -s , + register: dtoverlay + when: check_dtoverlay|success + changed_when: false -# If dtoverlay is set, but doesn't contain ds3231, append it to the end -- name: "Configure the 'dtoverlay' for ds3231" - replace: +# Set a new dtoverlay with i2c-rtc and ds3231 prepended +- name: Update 'dtoverlay' to include 'i2c-rtc' and 'ds3231' + lineinfile: dest: /boot/config.txt - regexp: '^dtoverlay=((?!.*ds3231).*i2c-rtc.*)' - replace: 'dtoverlay=\1,ds3231' + line: dtoverlay=i2c-rtc,ds3231,{{ dtoverlay.stdout }} + when: (check_dtoverlay|success) + and (dtoverlay|success) + and (dtoverlay.stdout is defined and dtoverlay.stdout != '') ... # vi: set ts=2 sts=2 sw=2 et ft=yaml: -- GitLab