| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package apps::automation::ansible::tower::custom::towercli; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use JSON::XS; |
| 26 |
|
| 27 |
sub new { |
| 28 |
my ($class, %options) = @_; |
| 29 |
my $self = {}; |
| 30 |
bless $self, $class; |
| 31 |
|
| 32 |
if (!defined($options{output})) { |
| 33 |
print "Class Custom: Need to specify 'output' argument.\n"; |
| 34 |
exit 3; |
| 35 |
} |
| 36 |
if (!defined($options{options})) { |
| 37 |
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); |
| 38 |
$options{output}->option_exit(); |
| 39 |
} |
| 40 |
|
| 41 |
if (!defined($options{noptions})) { |
| 42 |
$options{options}->add_options(arguments => { |
| 43 |
'hostname:s' => { name => 'hostname' }, |
| 44 |
'username:s' => { name => 'username' }, |
| 45 |
'password:s' => { name => 'password' }, |
| 46 |
'timeout:s' => { name => 'timeout', default => 50 }, |
| 47 |
'sudo' => { name => 'sudo' }, |
| 48 |
'command:s' => { name => 'command' }, |
| 49 |
'command-path:s' => { name => 'command_path' }, |
| 50 |
'command-options:s' => { name => 'command_options' }, |
| 51 |
'nossl' => { name => 'nossl' }, |
| 52 |
'proxyurl:s' => { name => 'proxyurl' } |
| 53 |
}); |
| 54 |
} |
| 55 |
$options{options}->add_help(package => __PACKAGE__, sections => 'TOWERCLI OPTIONS', once => 1); |
| 56 |
|
| 57 |
$self->{output} = $options{output}; |
| 58 |
|
| 59 |
return $self; |
| 60 |
} |
| 61 |
|
| 62 |
sub set_options { |
| 63 |
my ($self, %options) = @_; |
| 64 |
|
| 65 |
$self->{option_results} = $options{option_results}; |
| 66 |
} |
| 67 |
|
| 68 |
sub set_defaults {} |
| 69 |
|
| 70 |
sub check_options { |
| 71 |
my ($self, %options) = @_; |
| 72 |
|
| 73 |
if (defined($self->{option_results}->{proxyurl}) && $self->{option_results}->{proxyurl} ne '') { |
| 74 |
$ENV{HTTP_PROXY} = $self->{option_results}->{proxyurl}; |
| 75 |
$ENV{HTTPS_PROXY} = $self->{option_results}->{proxyurl}; |
| 76 |
} |
| 77 |
|
| 78 |
$self->{hostname} = (defined($self->{option_results}->{hostname})) && $self->{option_results}->{hostname} ne '' ? $self->{option_results}->{hostname} : undef; |
| 79 |
$self->{username} = (defined($self->{option_results}->{username})) && $self->{option_results}->{username} ne '' ? $self->{option_results}->{username} : undef; |
| 80 |
$self->{password} = (defined($self->{option_results}->{password})) && $self->{option_results}->{password} ne '' ? $self->{option_results}->{password} : undef; |
| 81 |
|
| 82 |
if (defined($self->{option_results}->{nossl})) { |
| 83 |
$ENV{TOWER_VERIFY_SSL} = 'no'; |
| 84 |
$self->{hostname} = 'http://' . $self->{hostname} if (defined($self->{hostname})); |
| 85 |
$ENV{HTTPS_PROXY} = $self->{option_results}->{proxyurl}; |
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
sub execute { |
| 92 |
my ($self, %options) = @_; |
| 93 |
|
| 94 |
centreon::plugins::misc::check_security_command( |
| 95 |
output => $self->{output}, |
| 96 |
command => $self->{option_results}->{command}, |
| 97 |
command_options => $self->{option_results}->{command_options}, |
| 98 |
command_path => $self->{option_results}->{command_path} |
| 99 |
); |
| 100 |
|
| 101 |
my $command = defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : 'tower-cli'; |
| 102 |
|
| 103 |
$self->{output}->output_add(long_msg => "Command line: '" . $command . " " . $options{cmd_options} . "'", debug => 1); |
| 104 |
|
| 105 |
my ($response) = centreon::plugins::misc::execute( |
| 106 |
output => $self->{output}, |
| 107 |
options => $self->{option_results}, |
| 108 |
sudo => $self->{option_results}->{sudo}, |
| 109 |
command => $command, |
| 110 |
command_path => $self->{option_results}->{command_path}, |
| 111 |
command_options => $options{cmd_options} |
| 112 |
); |
| 113 |
|
| 114 |
my $raw_results; |
| 115 |
|
| 116 |
eval { |
| 117 |
$raw_results = JSON::XS->new->utf8->decode($response); |
| 118 |
}; |
| 119 |
if ($@) { |
| 120 |
$self->{output}->output_add(long_msg => $response, debug => 1); |
| 121 |
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); |
| 122 |
$self->{output}->option_exit(); |
| 123 |
} |
| 124 |
|
| 125 |
return $raw_results; |
| 126 |
} |
| 127 |
|
| 128 |
sub tower_list_hosts_set_cmd { |
| 129 |
my ($self, %options) = @_; |
| 130 |
|
| 131 |
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); |
| 132 |
|
| 133 |
my $cmd_options = "host list --insecure --all-pages --format json"; |
| 134 |
$cmd_options .= " --tower-host '$self->{hostname}'" if (defined($self->{hostname})); |
| 135 |
$cmd_options .= " --tower-username '$self->{username}'" if (defined($self->{username})); |
| 136 |
$cmd_options .= " --tower-password '$self->{password}'" if (defined($self->{password})); |
| 137 |
$cmd_options .= " --group '$options{group}'" if (defined($options{group}) && $options{group} ne ''); |
| 138 |
$cmd_options .= " --inventory '$options{inventory}'" if (defined($options{inventory}) && $options{inventory} ne ''); |
| 139 |
|
| 140 |
return $cmd_options; |
| 141 |
} |
| 142 |
|
| 143 |
sub tower_list_hosts { |
| 144 |
my ($self, %options) = @_; |
| 145 |
|
| 146 |
my $cmd_options = $self->tower_list_hosts_set_cmd(%options); |
| 147 |
my $raw_results = $self->execute(cmd_options => $cmd_options); |
| 148 |
|
| 149 |
return $raw_results->{results}; |
| 150 |
} |
| 151 |
|
| 152 |
sub tower_list_inventories_set_cmd { |
| 153 |
my ($self, %options) = @_; |
| 154 |
|
| 155 |
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); |
| 156 |
|
| 157 |
my $cmd_options = 'inventory list --insecure --all-pages --format json'; |
| 158 |
$cmd_options .= " --tower-host '$self->{hostname}'" if (defined($self->{hostname})); |
| 159 |
$cmd_options .= " --tower-username '$self->{username}'" if (defined($self->{username})); |
| 160 |
$cmd_options .= " --tower-password '$self->{password}'" if (defined($self->{password})); |
| 161 |
|
| 162 |
return $cmd_options; |
| 163 |
} |
| 164 |
|
| 165 |
sub tower_list_inventories { |
| 166 |
my ($self, %options) = @_; |
| 167 |
|
| 168 |
my $cmd_options = $self->tower_list_inventories_set_cmd(%options); |
| 169 |
my $raw_results = $self->execute(cmd_options => $cmd_options); |
| 170 |
|
| 171 |
return $raw_results->{results}; |
| 172 |
} |
| 173 |
|
| 174 |
sub tower_list_projects_set_cmd { |
| 175 |
my ($self, %options) = @_; |
| 176 |
|
| 177 |
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); |
| 178 |
|
| 179 |
my $cmd_options = "project list --insecure --all-pages --format json"; |
| 180 |
$cmd_options .= " --tower-host '$self->{hostname}'" if (defined($self->{hostname})); |
| 181 |
$cmd_options .= " --tower-username '$self->{username}'" if (defined($self->{username})); |
| 182 |
$cmd_options .= " --tower-password '$self->{password}'" if (defined($self->{password})); |
| 183 |
|
| 184 |
return $cmd_options; |
| 185 |
} |
| 186 |
|
| 187 |
sub tower_list_projects { |
| 188 |
my ($self, %options) = @_; |
| 189 |
|
| 190 |
my $cmd_options = $self->tower_list_projects_set_cmd(%options); |
| 191 |
my $raw_results = $self->execute(cmd_options => $cmd_options); |
| 192 |
|
| 193 |
return $raw_results->{results}; |
| 194 |
} |
| 195 |
|
| 196 |
sub tower_list_job_templates_set_cmd { |
| 197 |
my ($self, %options) = @_; |
| 198 |
|
| 199 |
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); |
| 200 |
|
| 201 |
my $cmd_options = "job_template list --insecure --all-pages --format json"; |
| 202 |
$cmd_options .= " --tower-host '$self->{hostname}'" if (defined($self->{hostname})); |
| 203 |
$cmd_options .= " --tower-username '$self->{username}'" if (defined($self->{username})); |
| 204 |
$cmd_options .= " --tower-password '$self->{password}'" if (defined($self->{password})); |
| 205 |
|
| 206 |
return $cmd_options; |
| 207 |
} |
| 208 |
|
| 209 |
sub tower_list_job_templates { |
| 210 |
my ($self, %options) = @_; |
| 211 |
|
| 212 |
my $cmd_options = $self->tower_list_job_templates_set_cmd(%options); |
| 213 |
my $raw_results = $self->execute(cmd_options => $cmd_options); |
| 214 |
|
| 215 |
return $raw_results->{results}; |
| 216 |
} |
| 217 |
|
| 218 |
sub tower_list_schedules { |
| 219 |
my ($self, %options) = @_; |
| 220 |
|
| 221 |
$self->{output}->add_option_msg(short_msg => 'method unsupported (try to use --custommode=api)'); |
| 222 |
$self->{output}->option_exit(); |
| 223 |
} |
| 224 |
|
| 225 |
sub tower_list_unified_jobs { |
| 226 |
my ($self, %options) = @_; |
| 227 |
|
| 228 |
$self->{output}->add_option_msg(short_msg => 'method unsupported (try to use --custommode=api)'); |
| 229 |
$self->{output}->option_exit(); |
| 230 |
} |
| 231 |
|
| 232 |
sub tower_launch_job_template_set_cmd { |
| 233 |
my ($self, %options) = @_; |
| 234 |
|
| 235 |
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); |
| 236 |
|
| 237 |
my $cmd_options = "job launch --job-template=$options{launch_job_template_id} "; |
| 238 |
$cmd_options .= " --inventory '$options{launch_inventory}'" if (defined($options{launch_inventory})); |
| 239 |
$cmd_options .= " --credential '$options{launch_credential}'" if (defined($options{launch_credential})); |
| 240 |
$cmd_options .= " --tags '$options{launch_tags}'" if (defined($options{launch_tags})); |
| 241 |
$cmd_options .= " --limit '$options{launch_limit}'" if (defined($options{launch_limit})); |
| 242 |
$cmd_options .= " --extra-vars '$options{launch_extra_vars}'" if (defined($options{launch_extra_vars})); |
| 243 |
$cmd_options .= ' --insecure --format json'; |
| 244 |
$cmd_options .= " --tower-host '$self->{hostname}'" if (defined($self->{hostname})); |
| 245 |
$cmd_options .= " --tower-username '$self->{username}'" if (defined($self->{username})); |
| 246 |
$cmd_options .= " --tower-password '$self->{password}'" if (defined($self->{password})); |
| 247 |
|
| 248 |
return $cmd_options; |
| 249 |
} |
| 250 |
|
| 251 |
sub tower_launch_job_template { |
| 252 |
my ($self, %options) = @_; |
| 253 |
|
| 254 |
my $cmd_options = $self->tower_launch_job_template_set_cmd(%options); |
| 255 |
my $raw_results = $self->execute(cmd_options => $cmd_options); |
| 256 |
|
| 257 |
return $raw_results; |
| 258 |
} |
| 259 |
|
| 260 |
sub tower_get_job_set_cmd { |
| 261 |
my ($self, %options) = @_; |
| 262 |
|
| 263 |
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); |
| 264 |
|
| 265 |
my $cmd_options = "job get $options{job_id} --insecure --format json"; |
| 266 |
$cmd_options .= " --tower-host '$self->{hostname}'" if (defined($self->{hostname})); |
| 267 |
$cmd_options .= " --tower-username '$self->{username}'" if (defined($self->{username})); |
| 268 |
$cmd_options .= " --tower-password '$self->{password}'" if (defined($self->{password})); |
| 269 |
|
| 270 |
return $cmd_options; |
| 271 |
} |
| 272 |
|
| 273 |
sub tower_get_job { |
| 274 |
my ($self, %options) = @_; |
| 275 |
|
| 276 |
my $cmd_options = $self->tower_get_job_set_cmd(%options); |
| 277 |
my $raw_results = $self->execute(cmd_options => $cmd_options); |
| 278 |
|
| 279 |
return $raw_results; |
| 280 |
} |
| 281 |
|
| 282 |
1; |
| 283 |
|
| 284 |
__END__ |
| 285 |
|
| 286 |
=head1 NAME |
| 287 |
|
| 288 |
Ansible Tower CLI |
| 289 |
|
| 290 |
=head1 TOWERCLI OPTIONS |
| 291 |
|
| 292 |
Ansible Tower CLI |
| 293 |
|
| 294 |
To install the Tower CLI : https://docs.ansible.com/ansible-tower/latest/html/towerapi/tower_cli.html#installation |
| 295 |
|
| 296 |
=over 8 |
| 297 |
|
| 298 |
=item B<--hostname> |
| 299 |
|
| 300 |
Ansible Tower hostname (default uses setting in 'tower config'). |
| 301 |
|
| 302 |
=item B<--username> |
| 303 |
|
| 304 |
Ansible Tower username (default uses setting in 'tower config'). |
| 305 |
|
| 306 |
=item B<--password> |
| 307 |
|
| 308 |
Ansible Tower password (default uses setting in 'tower config'). |
| 309 |
|
| 310 |
=item B<--nossl> |
| 311 |
|
| 312 |
Use http connection. |
| 313 |
|
| 314 |
=item B<--timeout> |
| 315 |
|
| 316 |
Set timeout in seconds (default: 50). |
| 317 |
|
| 318 |
=item B<--sudo> |
| 319 |
|
| 320 |
Use 'sudo' to execute the command. |
| 321 |
|
| 322 |
=item B<--command> |
| 323 |
|
| 324 |
Command to get information (default: 'tower-cli'). |
| 325 |
Can be changed if you have output in a file. |
| 326 |
|
| 327 |
=item B<--command-path> |
| 328 |
|
| 329 |
Command path (default: none). |
| 330 |
|
| 331 |
=item B<--command-options> |
| 332 |
|
| 333 |
Command options (default: none). |
| 334 |
|
| 335 |
=item B<--proxyurl> |
| 336 |
|
| 337 |
Proxy URL if any |
| 338 |
|
| 339 |
=back |
| 340 |
|
| 341 |
=head1 DESCRIPTION |
| 342 |
|
| 343 |
B<custom>. |
| 344 |
|
| 345 |
=cut |