1Rex::Commands::Pkg(3) User Contributed Perl DocumentationRex::Commands::Pkg(3)
2
3
4
6 Rex::Commands::Pkg - Install/Remove Software packages
7
9 With this module you can install packages and files.
10
12 pkg "somepkg",
13 ensure => "present";
14 pkg "somepkg",
15 ensure => "latest",
16 on_change => sub {
17 say "package was updated.";
18 service someservice => "restart";
19 };
20 pkg "somepkg",
21 ensure => "absent";
22
24 pkg($package [, %options])
25 Since: 0.45
26
27 Use this resource to install or update a package. This resource will
28 generate reports.
29
30 pkg "httpd",
31 ensure => "latest", # ensure that the newest version is installed (auto-update)
32 on_change => sub { say "package was installed/updated"; };
33
34 pkg "httpd",
35 ensure => "absent"; # remove the package
36
37 pkg "httpd",
38 ensure => "present"; # ensure that some version is installed (no auto-update)
39
40 pkg "httpd",
41 ensure => "2.4.6"; # ensure that version 2.4.6 is installed
42
43 pkg "apache-server", # with a custom resource name
44 package => "httpd",
45 ensure => "present";
46
47 install($type, $data, $options)
48 The install function can install packages (for CentOS, OpenSuSE and
49 Debian) and files.
50
51 If you need reports, please use the pkg() resource.
52
53 installing a package (This is only supported on CentOS, OpenSuSE and
54 Debian systems.)
55 task "prepare", "server01", sub {
56 install package => "perl";
57
58 # or if you have to install more packages.
59 install package => [
60 "perl",
61 "ntp",
62 "dbus",
63 "hal",
64 "sudo",
65 "vim",
66 ];
67 };
68
69 installing a file
70 This is deprecated since 0.9. Please use File file instead.
71
72 task "prepare", "server01", sub {
73 install file => "/etc/passwd", {
74 source => "/export/files/etc/passwd",
75 owner => "root",
76 group => "root",
77 mode => 644,
78 };
79 };
80
81 installing a file and do something if the file was changed.
82 task "prepare", "server01", sub {
83 install file => "/etc/httpd/apache2.conf", {
84 source => "/export/files/etc/httpd/apache2.conf",
85 owner => "root",
86 group => "root",
87 mode => 644,
88 on_change => sub { say "File was modified!"; }
89 };
90 };
91
92 installing a file from a template.
93 task "prepare", "server01", sub {
94 install file => "/etc/httpd/apache2.tpl", {
95 source => "/export/files/etc/httpd/apache2.conf",
96 owner => "root",
97 group => "root",
98 mode => 644,
99 on_change => sub { say "File was modified!"; },
100 template => {
101 greeting => "hello",
102 name => "Ben",
103 },
104 };
105 };
106
107 This function supports the following hooks:
108
109 before
110 This gets executed before anything is done. All original parameters
111 are passed to it.
112
113 The return value of this hook overwrites the original parameters of
114 the function-call.
115
116 before_change
117 This gets executed right before the new package is installed. All
118 original parameters are passed to it.
119
120 This hook is only available for package installations. If you need
121 file hooks, you have to use the file() function.
122
123 after_change
124 This gets executed right after the new package was installed. All
125 original parameters, and the fact of change ("{ changed ="
126 TRUE|FALSE }>) are passed to it.
127
128 This hook is only available for package installations. If you need
129 file hooks, you have to use the file() function.
130
131 after
132 This gets executed right before the install() function returns. All
133 original parameters, and any returned results are passed to it.
134
135 remove($type, $package, $options)
136 This function will remove the given package from a system.
137
138 task "cleanup", "server01", sub {
139 remove package => "vim";
140 };
141
142 update_system
143 This function does a complete system update.
144
145 For example apt-get upgrade or yum update.
146
147 task "update-system", "server1", sub {
148 update_system;
149 };
150
151 If you want to get the packages that where updated, you can use the
152 on_change hook.
153
154 task "update-system", "server1", sub {
155 update_system
156 on_change => sub {
157 my (@modified_packages) = @_;
158 for my $pkg (@modified_packages) {
159 say "Name: $pkg->{name}";
160 say "Version: $pkg->{version}";
161 say "Action: $pkg->{action}"; # some of updated, installed or removed
162 }
163 };
164 };
165
166 Options for update_system
167
168 update_metadata
169 Set to TRUE if the package metadata should be updated. Since 1.5
170 default to FALSE if possible. Before 1.5 it depends on the package
171 manager.
172
173 update_package
174 Set to TRUE if you want to update the packages. Default is TRUE.
175
176 dist_upgrade
177 Set to TRUE if you want to run a dist-upgrade if your distribution
178 supports it. Default is FALSE.
179
180 installed_packages
181 This function returns all installed packages and their version.
182
183 task "get-installed", "server1", sub {
184
185 for my $pkg (installed_packages()) {
186 say "name : " . $pkg->{"name"};
187 say " version: " . $pkg->{"version"};
188 }
189
190 };
191
192 is_installed
193 This function tests if $package is installed. Returns 1 if true. 0 if
194 false.
195
196 task "isinstalled", "server01", sub {
197 if( is_installed("rex") ) {
198 say "Rex is installed";
199 }
200 else {
201 say "Rex is not installed";
202 }
203 };
204
205 update_package_db
206 This function updates the local package database. For example, on
207 CentOS it will execute yum makecache.
208
209 task "update-pkg-db", "server1", "server2", sub {
210 update_package_db;
211 install package => "apache2";
212 };
213
214 repository($action, %data)
215 Add or remove a repository from the package manager.
216
217 For Debian: If you have no source repository, or if you don't want to
218 add it, just remove the source parameter.
219
220 task "add-repo", "server1", "server2", sub {
221 repository "add" => "repository-name",
222 url => "http://rex.linux-files.org/debian/squeeze",
223 key_url => "http://rex.linux-files.org/DPKG-GPG-KEY-REXIFY-REPO"
224 distro => "squeeze",
225 repository => "rex",
226 source => 1;
227 };
228
229 To specify a key from a file use key_file => '/tmp/mykeyfile'.
230
231 To use a keyserver use key_server and key_id.
232
233 For ALT Linux: If repository is unsigned, just remove the sign_key
234 parameter.
235
236 task "add-repo", "server1", "server2", sub {
237 repository "add" => "altlinux-sisyphus",
238 url => "ftp://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus",
239 sign_key => "alt",
240 arch => "noarch, x86_64",
241 repository => "classic";
242 };
243
244 For CentOS, Mageia and SuSE only the name and the url are needed.
245
246 task "add-repo", "server1", "server2", sub {
247 repository add => "repository-name",
248 url => 'http://rex.linux-files.org/CentOS/$releasever/rex/$basearch/';
249
250 };
251
252 To remove a repository just delete it with its name.
253
254 task "rm-repo", "server1", sub {
255 repository remove => "repository-name";
256 };
257
258 You can also use one call to repository to add repositories on multiple
259 platforms:
260
261 task "add-repo", "server1", "server2", sub {
262 repository add => myrepo => {
263 Ubuntu => {
264 url => "http://foo.bar/repo",
265 distro => "precise",
266 repository => "foo",
267 },
268 Debian => {
269 url => "http://foo.bar/repo",
270 distro => "squeeze",
271 repository => "foo",
272 },
273 CentOS => {
274 url => "http://foo.bar/repo",
275 },
276 };
277 };
278
279 package_provider_for $os => $type;
280 To set another package provider as the default, use this function.
281
282 user "root";
283
284 group "db" => "db[01..10]";
285 package_provider_for SunOS => "blastwave";
286
287 task "prepare", group => "db", sub {
288 install package => "vim";
289 };
290
291 This example will install vim on every db server. If the server is a
292 Solaris (SunOS) it will use the blastwave Repositories.
293
294
295
296perl v5.38.0 2023-08-07 Rex::Commands::Pkg(3)