Forum: CFEngine Help
Subject: Re: /etc/resolv.conf: if updating, backup first
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,23954,23957#msg-23957
Matt,
First, you can avoid the wait (which is cf-agent's default ifelapsed period
between consecutive evaluations of the same promise) by using the -K option.
This is useful in cases like this when you are testing.
I got similar results using your given example - CFEngine edits the file every
time.
The problem is the way you are calling my_resolvconf. You have the following:
edit_line => my_resolvconf("${search}", "${nameservers}",
"${commented}"),
Since you are making reference to the slist variables with scalar notation
($(nameservers) instead of @(nameservers)), implicit looping is happening at
this point. Effectively, you are calling my_resolvconf multiple times on each
run, with all the different combinations of the list variables. Hence you get
the weird and seemingly random changes.
The solution is to pass the whole lists into the bundle, and let implicit
looping happen inside the bundle. This way the bundle gets called only one
time, and it expand the lists into their values. So you need to change the
edit_line line to this:
edit_line => my_resolvconf( "$(search)" , @(resolv.nameservers)
, @(resolv.commented) ),
You can see here that we are expanding $(search) as before, but @nameservers
and @commented are being passed as lists into the bundle (their names need to
be fully qualified so that the bundle can expand them correctly). Now we get
the desired behavior:
bash-3.2$ cat /tmp/resolv.conf
search mydomain.com
#nameserver 192.168.185.220
#nameserver 4.2.2.1
nameserver 192.168.184.7
nameserver 192.168.187.103
bash-3.2$ cf-agent -KI -f ./testresolv.cf
bash-3.2$ cf-agent -KI -f ./testresolv.cf
bash-3.2$ cf-agent -KI -f ./testresolv.cf
If you let CFEngine create the file from scratch, there's two steps to
convergence, while CFEngine gets the order right, but after that there are no
changes:
bash-3.2$ rm /tmp/resolv.conf*; touch /tmp/resolv.conf
bash-3.2$ cf-agent -KI -f ./testresolv.cf
-> Edited file /tmp/resolv.conf
bash-3.2$ cat /tmp/resolv.conf
search mydomain.com
nameserver 192.168.184.7
nameserver 192.168.187.103
#nameserver 192.168.185.220
#nameserver 4.2.2.1
bash-3.2$ cf-agent -KI -f ./testresolv.cf
-> Edited file /tmp/resolv.conf
bash-3.2$ cat /tmp/resolv.conf
search mydomain.com
#nameserver 192.168.185.220
#nameserver 4.2.2.1
nameserver 192.168.184.7
nameserver 192.168.187.103
bash-3.2$ cf-agent -KI -f ./testresolv.cf
bash-3.2$ cf-agent -KI -f ./testresolv.cf
bash-3.2$ cf-agent -KI -f ./testresolv.cf
Hope this helps.
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine