Ask Your Question

File lines replacement for multiple lines from an array?

asked 2016-10-12 10:29:44 -0600

Cos gravatar image

I'd like to do something that's a combination of file_line with match=>, and template expansion from an array variable.

Specifically, I'd like the config to be able to supply an array of nameserver IP addresses, and have that override the nameserver lines in /etc/resolv.conf. For example, if $nameservers = ['', ''], then it would replace any existing nameserver lines in resolv.conf with the two lines: nameserver nameserver

However, anything else in the resolv.conf file, such as the domain search list, and comments, should be left as is.

So, if this array has any values: - remove any existing nameserver lines from resolv.conf - add one nameserver line for each value in the array - leave all other lines in resolv.conf unchanged

If I use a template, I don't know how to preserve existing lines other than nameserver. If I use file_line, I don't know how to expand an array into a bunch of lines.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2016-10-12 11:00:24 -0600

Kevin T gravatar image

updated 2016-10-12 11:00:50 -0600

I assume you really should be including the other lines in your resolv.conf managed aswell with facts or hiera values. Surely you want the /etc/resolv.conf file to be entirely managed by puppet?

edit flag offensive delete link more


No, I do not. Not in this particular context. That doc just shows how to use a template to create resolv.conf, which as I explained is something I know how to do but wouldn't work for this use case.

Cos gravatar imageCos ( 2016-10-12 11:02:39 -0600 )edit

answered 2016-10-12 12:56:09 -0600

puser gravatar image

There are most likely better solutions but on a specific flag then you could run an exec that does some kind of regex to find any domain servers and remove them. After that you could simply append to the resolve.conf. I am not convinced this is best solution though.

edit flag offensive delete link more


Well, the workaround I'm thinking of is basically that: Write a python script that takes the values on the command line, and modifies resolv.conf. Then I could run it from an exec if $nameservers is not undef. But that's kludgey and kinda stepping outside puppet.

Cos gravatar imageCos ( 2016-10-12 13:18:37 -0600 )edit

The only other way is managing resolve.conf completely. That way you can provide each piece of the file via a template.

puser gravatar imagepuser ( 2016-10-12 13:27:29 -0600 )edit

Right, a template would prevent me from being able to preserve the rest of the file, as I noted in the question. file_lines with a "match" would work if I were just inserting one line. Shouldn't there be a way to do the same thing but insert multiple lines?

Cos gravatar imageCos ( 2016-10-12 13:37:53 -0600 )edit

answered 2016-10-18 10:46:25 -0600

pascal gravatar image

I have not used it myself, but your problem sounds like you want to use the augeas resource type. I think it already has a lens for resolv.conf, so it should be quiet simple.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-10-12 10:29:44 -0600

Seen: 187 times

Last updated: Oct 18 '16