LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-15-2016, 06:48 PM   #1
johnpedou
LQ Newbie
 
Registered: Dec 2016
Posts: 17

Rep: Reputation: Disabled
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".

Last edited by johnpedou; 12-15-2016 at 06:50 PM.
 
Old 12-15-2016, 07:35 PM   #2
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: arch
Posts: 668

Rep: Reputation: 190Reputation: 190
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

Last edited by TheEzekielProject; 12-15-2016 at 07:36 PM.
 
Old 12-15-2016, 08:16 PM   #3
johnpedou
LQ Newbie
 
Registered: Dec 2016
Posts: 17

Original Poster
Rep: Reputation: Disabled
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: `�'
 
Old 12-15-2016, 09:29 PM   #4
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: arch
Posts: 668

Rep: Reputation: 190Reputation: 190
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

Last edited by TheEzekielProject; 12-15-2016 at 09:39 PM.
 
Old 12-17-2016, 12:27 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by johnpedou View Post
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.
 
Old 12-17-2016, 04:15 AM   #6
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 421

Rep: Reputation: 74
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
 
Old 12-17-2016, 08:00 PM   #7
johnpedou
LQ Newbie
 
Registered: Dec 2016
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-17-2016, 08:59 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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!
 
  


Reply

Tags
bash scripting, puppet



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Puppet - JIRA module not connecting to mySQL sndlt Linux - Server 0 05-13-2015 12:45 AM
SELinux to allow puppet-server at https://puppet-server:8140 sndlt Linux - Security 8 05-04-2015 01:12 PM
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 08:03 AM
Connecting to remote server with a free shell -- what do you do next... honeybadger General 9 01-22-2012 12:15 PM
Parameter to execute shell script on puppet client through Puppet server niraj.kumar Linux - Server 3 02-08-2011 09:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration