Skip to content

Instantly share code, notes, and snippets.

@digglife
Last active June 20, 2018 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save digglife/bb75d834b01e97fe5fa6 to your computer and use it in GitHub Desktop.
Save digglife/bb75d834b01e97fe5fa6 to your computer and use it in GitHub Desktop.
Batch Script for deploying puppet agent
@ECHO OFF
REM Get Administrator Privilege on Windows 6+
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
IF '%errorlevel%' NEQ '0' (
ECHO Requesting administrative privileges...
GOTO UACPrompt
) ELSE ( GOTO gotAdmin )
:UACPrompt
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
ECHO UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
SET HOSTS_FILE=%WINDIR%\System32\drivers\etc\hosts
REM 32bit installer works for both x86 and x64 OS.
SET PUPPET_INSTALLER=puppet.msi
SET PUPPET_BIN_PATH=%PROGRAMFILES%\Puppet Labs\Puppet\bin
REM Install path is different on X64 OS.
REM https://docs.puppetlabs.com/pe/latest/install_windows.html#program-directory
IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
REM Quote the whole set statement or the ")" in the path will be interpreted.
REM http://stackoverflow.com/a/7886505/1350657
SET "PUPPET_BIN_PATH=%PROGRAMFILES(X86)%\Puppet Labs\Puppet\bin"
)
IF EXIST "%PROGRAMDATA%" (
SET PUPPET_DATA=%PROGRAMDATA%\PuppetLabs\puppet
) ELSE (
SET PUPPET_DATA=%ALLUSERSPROFILE%\Application Data\PuppetLabs\puppet
)
SET PUPPET_CONFIG=%PUPPET_DATA%\etc\puppet.conf
SET PUPPET_SSL=%PUPPET_DATA%\etc\ssl
IF EXIST "%PUPPET_BIN_PATH%" (
ECHO [WARN] Puppet has already been installed. Skip...
GOTO :EOF
)
REM Puppet command is not available by default.
SET PATH=%PATH%;C:\Windows\System32;%PUPPET_BIN_PATH%
REM Add host entry for puppet master.
VERIFY > NUL
TYPE "%HOSTS_FILE%" | FINDSTR puppet > NUL 2>&1
IF ERRORLEVEL 1 (
ECHO [INFO] Adding host entry for puppet master...
REM Add a newline first in case.
>>"%HOSTS_FILE%" ECHO.
>>"%HOSTS_FILE%" ECHO 10.0.11.111 puppet
)
VERIFY > NUL
REM Map Windows Share Driver to Z:
net use * /DELETE /Y >NUL 2>&1
net use Z: \\10.0.11.112\package >NUL 2>&1
IF ERRORLEVEL 1 (
ECHO [CRITICAL] Unable to Map Network Drive for Puppet Installers.
GOTO :EOF
)
ECHO [INFO] Start installing puppet agent with [%PUPPET_INSTALLER%] ...
REM Install Puppet Agent without automatically starting it.
msiexec /qn /i "Z:\%PUPPET_INSTALLER%" PUPPET_AGENT_STARTUP_MODE=Manual
REM Configure Puppet settings.
REM puppet config command can't be executed in sequence. WTF?!
REM puppet config set node_name facter --section agent
REM puppet config set node_name_fact ipaddress --section agent
REM puppet config set refresh_interval 4h --section agent
REM puppet config set stringify_facts false --section main
ECHO Configuring puppet settings..
> "%PUPPET_CONFIG%" ECHO [main]
>> "%PUPPET_CONFIG%" ECHO server=puppet
>> "%PUPPET_CONFIG%" ECHO pluginsync=true
>> "%PUPPET_CONFIG%" ECHO autoflush=true
>> "%PUPPET_CONFIG%" ECHO environment=production
>> "%PUPPET_CONFIG%" ECHO stringify_facts = false
>> "%PUPPET_CONFIG%" ECHO [agent]
>> "%PUPPET_CONFIG%" ECHO node_name = facter
>> "%PUPPET_CONFIG%" ECHO node_name_fact = ipaddress
>> "%PUPPET_CONFIG%" ECHO refresh_interval = 4h
REM Change Puppet Service Startup Mode to automatic.
sc config puppet start= auto >NUL 2>&1
net use * /DELETE /Y >NUL 2>&1
REM ECHO Run Puppet Agent for the first time...
REM puppet agent -t
REM Ensure puppet server is always running.
ECHO [INFO] Starting Puppet Service...
"%PUPPET_BIN_PATH%\puppet.bat" resource service puppet ensure=running enable=true
GOTO :EOF
:EOF
ECHO [INFO] DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment