enh(plugin): add ansible host IP to Ansible Tower discovery (#5221)

REFS: CTOR-311
This commit is contained in:
sdepassio 2024-10-07 11:04:16 +02:00 committed by GitHub
parent 06b1fe2731
commit 591981bf37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 4 deletions

View File

@ -30,7 +30,7 @@ sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'group' => { name => 'group' },
'inventory' => { name => 'inventory' },
@ -54,7 +54,7 @@ sub run {
$disco_stats->{start_time} = time();
my $hosts = $options{custom}->tower_list_hosts(
group => $self->{option_results}->{group},
group => $self->{option_results}->{group},
inventory => $self->{option_results}->{inventory}
);
@ -71,6 +71,14 @@ sub run {
$host{inventory_name} = $host->{summary_fields}->{inventory}->{name};
$host{groups} = $host->{summary_fields}->{groups}->{results};
$host{enabled} = $host->{enabled};
# Get the ansible host IP address if available
eval {
my $json = decode_json($host->{variables});
$host{ansible_host} = $json->{ansible_host} || '';
};
if ($@) {
$host{ansible_host} = '';
}
push @disco_data, \%host;
}
@ -88,7 +96,7 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,43 @@
*** Settings ***
Documentation Check the discovery mode with api custom mode
Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resource
Suite Setup Start Mockoon ${MOCKOON_JSON}
Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}ansible_tower.json
${CMD} ${CENTREON_PLUGINS}
... --plugin=apps::automation::ansible::tower::plugin
... --custommode=api
... --hostname=${HOSTNAME}
... --username=username
... --password=password
... --port=${APIPORT}
... --mode=discovery
*** Test Cases ***
Discovery ${tc}
[Tags] apps automation ansible api
${command} Catenate
... ${CMD}
... ${extraoptions}
${output} Run ${command}
${output} Strip String ${output}
Should Be Equal As Strings
... ${output}
... ${expected_result}
... Wrong output result for command:\n${CMD}\n\nObtained:\n${output}\n\nExpected:\n${expected_result}\n
... values=False
... collapse_spaces=True
Examples: tc extraoptions expected_result --
... 1 | jq '.results | length' 10
... 2 | jq -r '.results | map(select(.ansible_host != "" and (.ansible_host | test("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$") | not))) | .[].ansible_host' ${EMPTY}