templates: hostfile-setup: Improve regexp for host entries

A hosts line may contain tabs instead of spaces and the '\ .*' regexp
doesn't match on that case. As such, we can improve the regexp to simply
look for any character after matching the address word. This fixes the
following problem when an entry with a tab exists in the file

~$ cat -Et /etc/hosts|head -n1
127.0.0.1^Ilocalhost$

The entry is not updated by the script leading to the following problem

fatal: [localhost]: FAILED! => {
    "assertion": "'127.0.1.1 localhost.openstack.local localhost' in hosts_content",
    "changed": false,
    "evaluated_to": false
}

Change-Id: I23d22cbf4317a3c9228bcdb6b3e9e6c2238fcfb7
This commit is contained in:
Markos Chandras
2018-01-23 10:55:58 +00:00
parent 9de38169a4
commit 32e44dc7bc

View File

@@ -10,7 +10,7 @@ function insert_host_entry {
sed -i "/^${ADDR}\b/d" /etc/hosts
echo "${ENTRY}" | tee -a /etc/hosts
elif grep -q "^${ADDR}\b" /etc/hosts; then
sed -i "s|^${ADDR}\b\ .*|${ENTRY}|" /etc/hosts
sed -i "s|^${ADDR}\b.*|${ENTRY}|" /etc/hosts
elif ! grep -q "^${ENTRY}$" /etc/hosts; then
echo "${ENTRY}" | tee -a /etc/hosts
fi