Auto deploy Puppet Agent


Continue with my previous Puppet and Puppet dashboard installation, now I start to deploy the agent to all my Ubuntu desktops. I wrote a script to do the dirty work.
Pre-requisite package: expect (no need if use passwordless ssh, but you need to change the script slightly).
How to use:
1) Create the following 4 scripts (sshlogin.exp, scplogin.exp, install_puppet and deploy_puppet), and make them executable.
2) Add all IP address into the IP.list file, only one IP in each line.
3) Run ./deploy_puppet, then go to get a cup of coffee 🙂
+++++++++++++++++++++++++++++
#!/usr/bin/expect -f
# sshlogin.exp
# syntax:  ./sshlogin.exp password ip command
# set Variables
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
# replace jchen with your username
spawn ssh -o StrictHostKeyChecking=no jchen@$ipaddr $scriptname $arg1
match_max 100000
# Look for passwod prompt
expect “*?assword:*”
# Send password aka $password
send — “$password”
# send blank line () to make sure we get back to gui
send — “”
expect eof
+++++++++++++++++++++++++++++
#!/usr/bin/expect -f
# scplogin.exp
# syntax: ./sshlogin.exp password ip filename
# set Variables
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set filename [lrange $argv 2 2]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
# replace jchen with your username
spawn scp $filename jchen@$ipaddr:~/
match_max 100000
# Look for passwod prompt
expect “*?assword:*”
# Send password aka $password
send — “$password”
# send blank line () to make sure we get back to gui
send — “”
expect eof
+++++++++++++++++++++++++++++
#!/bin/bash
# install_puppet
#install puppet
echo N  | sudo apt-get install -y puppet
#start puppet on boot
sudo puppet resource service puppet ensure=running enable=true
sudo puppet agent –test
sudo chmod 666 /etc/default/puppet
sudo echo START=yes > /etc/default/puppet
sudo echo DAEMON_OPTS=”” >> /etc/default/puppet
#configure the server in the conf file
agent=$(hostname | tr ‘[A-Z]’ ‘[a-z]’)
sudo touch /etc/puppet/puppet.conf
sudo chmod 666 /etc/puppet/puppet.conf
sudo echo [main] > /etc/puppet/puppet.conf
# replace CentOS.my.lab with your Master server name
sudo echo server=CentOS.my.lab >> /etc/puppet/puppet.conf
sudo echo certname=$agent >> /etc/puppet/puppet.conf
sudo echo logdir=/var/log/puppet >> /etc/puppet/puppet.conf
sudo echo vardir=/var/lib/puppet >> /etc/puppet/puppet.conf
sudo echo ssldir=/var/lib/puppet/ssl >> /etc/puppet/puppet.conf
sudo echo rundir=/var/run/puppet >> /etc/puppet/puppet.conf
sudo echo factpath=$vardir/lib/facter >> /etc/puppet/puppet.conf
sudo echo templatedir=$confdir/templates >> /etc/puppet/puppet.conf
sudo echo prerun_command=/etc/puppet/etckeeper-commit-pre >> /etc/puppet/puppet.conf
sudo echo postrun_command=/etc/puppet/etckeeper-commit-post >> /etc/puppet/puppet.conf
#restart puppet
sudo puppet agent
+++++++++++++++++++++++++++++++
#!/bin/bash
# deploy_puppet
# check if IP pingable
for ip in $(cat IP.list)
do
if  ping -c 4 $ip > /dev/null 2>&1
then
echo $ip ‘is pingable’
#check /etc/puppet exisitence
 if ./sshlogin.exp password $ip ls /etc | grep puppet > /dev/null 2>&1
 then
 echo ‘Puppet is already installed on’ $ip
 else
 #copy install script to remote host
 ./scplogin.exp password $ip install_puppet
 #install puppet on remote host
 ./sshlogin.exp password $ip /home/jchen/install_puppet
 echo $ip >> IP.success
 fi
else
echo $ip ‘is NOT pingable’
echo $ip >> IP.not_pingable
fi
done
download_icon
Advertisements

One thought on “Auto deploy Puppet Agent

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s