If we think about the set of a certain type of resources on our system as being part of a list, we can tell Puppet that we want to explicitly manage the whole set. In other words, if Puppet isn't managing a resource in that set then it will be removed. This clearly only works when that set is a list of finite length, such as the list of packages or the list of users.
There is a finite set of host records stored in /etc/hosts
. Create a Puppet manifest that represents them using the output of a puppet resource
command. Then use a resources
resource to purge unmanaged host entries and experiment to see the effects this has.
If you damage the hosts file and cannot reach the master, let the instructor know and they will help you restore it.
Tip: You can redirect the output of a command to a file, if it'sconvenient. For example, puppet resource host > hosts.pp
.
Change the current working directory to the environmentpath
cd $(puppet agent --configprint environmentpath)/production/modules
-
Create a new PDK module called
system
$ pdk new module
You will see several questions requiring an answer. Enter these answers:
Replace the “N” in these answers with your student number (for example,
student8
).Question Answer Module Name system
Forge Name studentN
Credit author Student N
License Apache-2.0
Operating systems RedHat -
Press
Y
to generate the module in the current directory. -
Change the current working directory to the module you created.
cd system
-
Gather the current hosts by running
puppet resource host
-
Turn the output of the current hosts into a Puppet class file.
pdk new class hosts
- Edit
manifests/hosts.pp
- Paste the output of the
puppet resource host
command into the class.
-
Add a
resources
resource to the class.- Set the
purge => true
attribute.
- Set the
-
Validate and test the new class.
pdk validate
-
Commit your changes to the repo.
- Init your module:
git init
- Add the origin:
git remote add origin [email protected]:puppet/system.git
- Checkout the branch:
git checkout -b studentN
- Add the files:
git add --all
- Create a commit:
git commit -m "Initial Commit"
- Push the commit to the Git server:
git push origin studentN
- Init your module:
-
Change directories to
[control-repo]
-
Edit the
Puppetfile
and uncomment out these lines.mod 'system', :git => '[email protected]:puppet/system.git', :branch => 'studentN'
-
-
Commit and push your control repo changes, using the git add/commit/push sequence.
-
Point your browser at https://classXXXX-master.classroom.puppet.com/
-
Enter the credentials:
- username: studentN
- password: puppetlabs
-
In the left menu, under CONFIGURE, click Classification
-
Open
All Environments
and yourstudentN-env
-
Click on the
Configuration
tab -
Under
Classes
section,Add new class
type insystem::hosts
andAdd class
-
Click Commit 1 change in the lower right corner.
-
Add a new entry in
/etc/hosts
namedremoveme.puppetlabs.vm
- Add
1.2.3.4 removeme.puppetlabs.vm
Sample
/etc/hosts
file:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.120.0.235 puppet.classroom.puppet.com puppet 10.120.0.51 gitlab.classroom.puppet.com gitlab gitlab.puppet.vm 1.2.3.4 removeme.puppetlabs.vm
- Add
-
Run puppet to purge your change:
puppet agent -t
Expected output:[root@1905nix0 control-repo]# puppet agent -t ... Info: Caching catalog for 1905nix0.classroom.puppet.com Info: Applying configuration version 'puppet-student0-5862f9c2c38' Notice: /Stage[main]/System::Hosts/Host[removeme.puppetlabs.vm]/ensure: removed Info: Computing checksum on file /etc/hosts Notice: Applied catalog in 0.29 seconds
[root@training modules]# tree system/
system/
├── examples
│ └── hosts.pp
└── manifests
└── hosts.pp
class system::hosts {
resources {'host':
purge => true,
}
host { 'puppet.classroom.puppet.com':
ensure => present,
host_aliases => ['puppet'],
ip => '192.168.X.X', ## use the IP of the classroom PE server
}
host { 'gitlab.classroom.puppet.com':
ensure => present,
host_aliases => ['gitlab'],
ip => '192.168.X.X', ## use the IP of the classroom Gitlab server
}
host { 'localhost':
ensure => present,
host_aliases => [
'localhost.localdomain',
'localhost4',
'localhost4.localdomain4',
'training.puppetlabs.vm',
'training'
],
ip => '127.0.0.1',
}
## Use your own hostname, alias and IP, or use facts from facter.
host { $::fqdn:
ensure => present,
host_aliases => $::hostname,
ip => $::ipaddress,
}
}
include system::hosts
| Previous Lab | Next Lab |