LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Connecting to Puppet Server with Shell Scripting (https://www.linuxquestions.org/questions/linux-newbie-8/connecting-to-puppet-server-with-shell-scripting-4175595521/)

johnpedou 12-15-2016 06:48 PM

Connecting to Puppet Server with Shell Scripting
 
Hello, I am trying to write a shell script that will automatically install puppet agent and then connect to the puppet host. I was able to successfully get the script to install puppet. However, I am currently stuck because I don't know how to write part of the script to edit the puppet.conf file.

I wrote the line
Quote:

sudo vi +15 /etc/puppet/puppet.conf
to go to the specific line that I need to edit in the puppet configuration file. From there, I need to add the line "server = 'server_hostname'" so that the server's hostname is added to the file. But I don't know how to 1. Get the server's hostname from the script and 2. Add the line in the script that writes "server = server_hostname".

TheEzekielProject 12-15-2016 07:35 PM

As far as writing to the file, this sounds like a job for sed

e.g.

sed -i ‘15iserver = server_hostname’ FILE

-i means modify the specified file in place, the 15i inserts "server = server_hostname" at line 15.

as far as grabbing the name from the script i imagine youd just have to store the hostname as a variable somewhere to grab it from later.

Im no expert so please take that advice with a grain of salt lol

johnpedou 12-15-2016 08:16 PM

Thank you for your response. I believe you were correct with the command sed. But I don't think the one you posted was formatted correctly. When I put it into my script and ran it, I got an error:
Quote:

sed: -e expression #1, char 1: unknown command: `�'

TheEzekielProject 12-15-2016 09:29 PM

Try more like sed -i '15iserver=server_hostname' FILE

This worked on my file as long as there is there at least >= as the line at which you are trying to insert.

sed doesn't seem to like my spaces. I'm not super familiar with the command, only light use. Note that this will print literally

server=server_hostname

on line 15. I'm not exactly sure how you would pull that variable in though. I think that is probably a job for echo but again I'm not super familiar with it yet either haha

szboardstretcher 12-17-2016 12:27 AM

Quote:

Originally Posted by johnpedou (Post 5642673)
Thank you for your response. I believe you were correct with the command sed. But I don't think the one you posted was formatted correctly. When I put it into my script and ran it, I got an error:

It's formatted correctly. But your copy/paste is adding messed up control characters between windows/mac and the linux terminal.

Try typing it from scratch.

c0wb0y 12-17-2016 04:15 AM

Problem with using 'line 15' as basis for the insertion, is that it is arbitrary. There is a high chance that if the user adds/deletes lines then you would end up inserting it in places you don't want to. Instead, you can use some familiar lines like certname or environment to base it from. For example:

Code:

sed -i '/^\s*certname/a \\tserver = server_hostname' puppet.conf

johnpedou 12-17-2016 08:00 PM

Quote:

if the user adds/deletes lines then you would end up inserting it in places you don't want to
Well that isn't really an issue because this script is set to run automatically when a user starts up their machine so they wouldn't have installed puppet or opened up the configuration file yet. I simply changed it using
Quote:

echo " server = hostname">> etc/puppet/puppet.conf
and that ended up working. Thank you everyone for responding

szboardstretcher 12-17-2016 08:59 PM

Was it because you typed it in instead of copy/pasted?

If so, there are usually changes you can make to your emulator/shell/editor that will cause it to refuckulate correctly and stop causing \r and ^M pasting errors. If that is something you want to figure out, start a new thread and let us know your OS, text editor and shell, and we could help out.

Good luck!


All times are GMT -5. The time now is 02:45 AM.