Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LSO
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
GÉANT Orchestration and Automation Team
GAP
LSO
Commits
71de4969
Commit
71de4969
authored
1 year ago
by
Karel van Klink
Browse files
Options
Downloads
Patches
Plain Diff
Split off parsing of playbook output to a separate method
parent
7efb36eb
No related branches found
No related tags found
1 merge request
!42
less verbose Nokia output
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lso/playbook.py
+34
-23
34 additions, 23 deletions
lso/playbook.py
with
34 additions
and
23 deletions
lso/playbook.py
+
34
−
23
View file @
71de4969
...
...
@@ -4,6 +4,7 @@ import json
import
logging
import
threading
import
uuid
from
typing
import
Any
import
ansible_runner
import
requests
...
...
@@ -60,27 +61,14 @@ def playbook_launch_error(reason: str) -> PlaybookLaunchResponse:
return
PlaybookLaunchResponse
(
status
=
PlaybookJobStatus
.
ERROR
,
info
=
reason
)
def
_
run_playbook_proc
(
job_id
:
str
,
playbook_path
:
str
,
extra_vars
:
dict
,
inventory
:
list
[
str
],
callback
:
str
)
->
None
:
"""
Run a playbook, internal function
.
def
_
process_json_output
(
runner
:
ansible_runner
.
Runner
)
->
list
[
dict
[
Any
,
Any
]]:
# ignore: C901
"""
Handle Ansible runner output, and filter out redundant an overly verbose messages
.
:param str job_id: Identifier of the job that
'
s executed.
:param str playbook_path: Ansible playbook to be executed.
:param dict extra_vars: Extra variables passed to the Ansible playbook.
:param str callback: Callback URL to PUT to when execution is completed.
:param [str] inventory: Ansible inventory to run the playbook against.
:param ansible_runner.Runner runner: The result of an Ansible playbook execution.
:return: A filtered dictionary that contains only the relevant parts of the output from executing an Ansible
playbook.
:rtype: list[dict[Any, Any]]
"""
ansible_playbook_run
=
ansible_runner
.
run
(
playbook
=
playbook_path
,
inventory
=
inventory
,
extravars
=
extra_vars
,
json_mode
=
True
,
)
# Process playbook JSON stdout
json_output
=
ansible_playbook_run
.
stdout
json_content
=
json_output
.
read
()
parsed_output
=
[]
too_verbose_keys
=
[
"
_ansible_no_log
"
,
"
_ansible_delegated_vars
"
,
...
...
@@ -93,11 +81,14 @@ def _run_playbook_proc(job_id: str, playbook_path: str, extra_vars: dict, invent
"
include_args
"
,
"
server_capabilities
"
,
]
json_content
=
runner
.
stdout
.
read
()
parsed_output
=
[]
for
line
in
json_content
.
strip
().
splitlines
():
try
:
task_output
=
json
.
loads
(
line
)
if
"
res
"
in
task_output
[
"
event_data
"
]
and
(
int
(
ansible_playbook_
run
.
rc
)
!=
0
or
task_output
[
"
event_data
"
][
"
res
"
][
"
changed
"
]
is
True
int
(
run
ner
.
rc
)
!=
0
or
task_output
[
"
event_data
"
][
"
res
"
][
"
changed
"
]
is
True
):
# The line contains result data, and must either consist of a change, or the playbook failed, and we
# want all steps, including those that didn't make changes.
...
...
@@ -108,9 +99,8 @@ def _run_playbook_proc(job_id: str, playbook_path: str, extra_vars: dict, invent
task_result
.
pop
(
remove
,
None
)
# Remove meta-steps that just copy some temporary files, and continue to the next event
if
"
state
"
in
task_result
:
if
task_result
[
"
state
"
]
==
"
directory
"
or
task_result
[
"
state
"
]
==
"
file
"
:
continue
if
"
state
"
in
task_result
and
(
task_result
[
"
state
"
]
==
"
directory
"
or
task_result
[
"
state
"
]
==
"
file
"
):
continue
if
"
diff_lines
"
in
task_result
:
# Juniper-specific
...
...
@@ -137,6 +127,27 @@ def _run_playbook_proc(job_id: str, playbook_path: str, extra_vars: dict, invent
# If the line can't be decoded as JSON, include it in its entirety.
parsed_output
.
append
({
"
invalid_json
"
:
line
})
return
parsed_output
def
_run_playbook_proc
(
job_id
:
str
,
playbook_path
:
str
,
extra_vars
:
dict
,
inventory
:
list
[
str
],
callback
:
str
)
->
None
:
"""
Run a playbook, internal function.
:param str job_id: Identifier of the job that
'
s executed.
:param str playbook_path: Ansible playbook to be executed.
:param dict extra_vars: Extra variables passed to the Ansible playbook.
:param str callback: Callback URL to PUT to when execution is completed.
:param [str] inventory: Ansible inventory to run the playbook against.
"""
ansible_playbook_run
=
ansible_runner
.
run
(
playbook
=
playbook_path
,
inventory
=
inventory
,
extravars
=
extra_vars
,
json_mode
=
True
,
)
parsed_output
=
_process_json_output
(
ansible_playbook_run
)
payload
=
[
{
"
pp_run_results
"
:
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment