Vagrant – Create a Puppet Server and Agent


After learning how to install Puppet – Foreman and Provisioning in Vagrant we will give here an example. In this example we will create two virtual machines:
– One of them will be the Puppet server with Foreman and the other will be a computer with Puppet agent.
– After everything is completed we will go to Foreman and check if everything works as intended.

Vagrantfile:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define "box1" do |box1|
     box1.vm.box = "ubuntu/trusty64"
     box1.vm.hostname = "puppetmaster.example.com"
     box1.vm.network "forwarded_port", guest: 443, host:4443
     box1.vm.provider "virtualbox" do |vb|
       vb.customize ["modifyvm", :id, "--memory", "2048"]
     end
     box1.vm.provision "shell", path: "puppetinstall.sh"
     box1.vm.network "private_network", ip: "192.168.33.10", adapter: 2
  end
  config.vm.define "box2" do |box2|
     box2.vm.box = "precise64"
     box2.vm.hostname = "ubuntutest"
     box2.vm.network "private_network", ip: "192.168.33.11", adapter: 2
     box2.vm.provision "shell", path: "puppetagentinstall.sh"
  end

end

In the example above we define
– Two machines,
– A host only network for the two machines to interact,
– Their hostnames
– Port forwarding to connect to Foreman
– RAM and CPU details of the Puppet server
– Provisioning files of the machines

puppetinstall.sh

This is the code to install Puppet and Foreman. Refer to “Puppet and Foreman Installation” if you need more details.

wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb
sudo dpkg -i puppetlabs-release-trusty.deb
echo "deb http://deb.theforeman.org/ trusty stable" | sudo tee /etc/apt/sources.list.d/foreman.list
echo "deb http://deb.theforeman.org/ plugins stable" | sudo tee --append /etc/apt/sources.list.d/foreman.list
wget -q http://deb.theforeman.org/pubkey.gpg -O- | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y puppetmaster puppet
sudo sed -i s/START=no/START=yes/g /etc/default/puppetmaster
sudo sed -i s/\ localhost/\ puppetmaster.example.com/g /etc/hosts
sudo service puppetmaster restart
sudo sed -i '/\/var\/log\/puppet/a \server=puppetmaster.example.com' /etc/puppet/puppet.conf
sudo sed -i s/START=no/START=yes/g /etc/default/puppet
sudo puppet agent --enable
sudo service puppet restart
sudo apt-get install -y apache2 foreman-installer
sudo foreman-installer > /tmp/foreman.log
sudo puppet agent --test

 puppetagentinstall.sh

This code install the puppet agent and makes the necessary configuration:
– First 4 lines installs the agent.
– 5. line adds the IP and FQDN of the server to hosts file
– 6. line adds the server details to the puppet.conf file
– 7. line enables the puppet agent service
– 8. line restart the service
– 9. line run puppet agent in test mode. This will ensure that it tries to connect to server and a certificate will be issued.

wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb
sudo dpkg -i puppetlabs-release-trusty.deb
sudo apt-get update
sudo apt-get install -y puppet
sudo echo 192.168.33.10 puppetmaster.example.com puppetmaster | sudo tee --append /etc/hosts
sudo sed -i '/\/var\/log\/puppet/a \server=puppetmaster.example.com' /etc/puppet/puppet.conf
sudo sed -i s/START=no/START=yes/g /etc/default/puppet
sudo service puppet restart
sudo puppet agent --test

After creating all of the files. Run “vagrant up” and take a cup of coffee.

After all of the setup finishes: run “vagrant ssh box1” command  and “cat /tmp/foreman.log” to see the foreman password. And go to https://localhost:4443 in your host machine. Use admin and the password in the log file to login.

After logging in check the smart proxy configuration under “Infrastructure --> Smart Proxies ” and click “Certificates“.

foreman smart proxy

There you will see a list of connected agents. The agents that are not signed yet will be seen with “Sign” option.

Vagrant - Create a Puppet Server and Agent

Click “Sign” and  goto “Hosts --> All hosts” if you do not see the newly signed client it will be available when it connects to server. If you one to see immediately. ssh to box2 and run “sudo puppet agent --test” after that refresh the hosts list and you will see the new client.

Troubleshooting:
If there are any certificate mismatch errors:
– Delete the certificate from smart proxy and also from client machine.
– To delete from client in our example use “sudo find /var/lib/puppet/ssl -name ubuntutest.pem -delete“.
– Lastly run “sudo puppet agent --test” in client again, sign it again in smart proxy. Run “sudo puppet agent --test” one last time in box2 and see if you see the screen below:

foreman host list

This post is completed here.

Links:
– http://theforeman.org
– http://puppetlabs.com

MyFreeCopyright.com Registered & Protected