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
447f301d
Verified
Commit
447f301d
authored
1 year ago
by
Karel van Klink
Browse files
Options
Downloads
Patches
Plain Diff
remove JSON interpretation of ansible runner output
parent
0695ae1b
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!71
Publish LSO version 1.0
,
!68
remove JSON interpretation of ansible runner output
Pipeline
#85091
failed
1 year ago
Stage: tox
Stage: documentation
Stage: trigger_jenkins_build
Changes
4
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
config.json.example
+1
-6
1 addition, 6 deletions
config.json.example
lso/config.py
+4
-2
4 additions, 2 deletions
lso/config.py
lso/playbook.py
+2
-55
2 additions, 55 deletions
lso/playbook.py
test/conftest.py
+1
-4
1 addition, 4 deletions
test/conftest.py
with
8 additions
and
67 deletions
config.json.example
+
1
−
6
View file @
447f301d
{
{
"ansible_playbooks_root_dir": "/app/gap/ansible/ansible_collections/geant/gap_ansible/playbooks",
"ansible_playbooks_root_dir": "/app/gap/ansible/ansible_collections/geant/gap_ansible/playbooks"
"filtered_ansible_keys": [
"_ansible_no_log",
"_ansible_delegated_vars",
"_ansible_verbose_always"
]
}
}
This diff is collapsed.
Click to expand it.
lso/config.py
+
4
−
2
View file @
447f301d
...
@@ -19,7 +19,7 @@ CONFIG_SCHEMA = {
...
@@ -19,7 +19,7 @@ CONFIG_SCHEMA = {
"
ansible_playbooks_root_dir
"
:
{
"
type
"
:
"
string
"
},
"
ansible_playbooks_root_dir
"
:
{
"
type
"
:
"
string
"
},
"
filtered_ansible_keys
"
:
{
"
type
"
:
"
array
"
,
"
items
"
:
{
"
type
"
:
"
string
"
}},
"
filtered_ansible_keys
"
:
{
"
type
"
:
"
array
"
,
"
items
"
:
{
"
type
"
:
"
string
"
}},
},
},
"
required
"
:
[
"
ansible_playbooks_root_dir
"
,
"
filtered_ansible_keys
"
],
"
required
"
:
[
"
ansible_playbooks_root_dir
"
],
"
additionalProperties
"
:
False
,
"
additionalProperties
"
:
False
,
}
}
DEFAULT_REQUEST_TIMEOUT
=
10
DEFAULT_REQUEST_TIMEOUT
=
10
...
@@ -33,7 +33,9 @@ class Config(BaseModel):
...
@@ -33,7 +33,9 @@ class Config(BaseModel):
"""
"""
ansible_playbooks_root_dir
:
str
ansible_playbooks_root_dir
:
str
filtered_ansible_keys
:
list
[
str
]
#: .. deprecated:: 0.21
#: Not used anymore, does not have to be present in config.
filtered_ansible_keys
:
list
[
str
]
|
None
=
None
def
load_from_file
(
file
:
Path
)
->
Config
:
def
load_from_file
(
file
:
Path
)
->
Config
:
...
...
This diff is collapsed.
Click to expand it.
lso/playbook.py
+
2
−
55
View file @
447f301d
"""
Module that gathers common API responses and data models.
"""
"""
Module that gathers common API responses and data models.
"""
import
json
import
logging
import
logging
import
threading
import
threading
import
uuid
import
uuid
...
@@ -44,56 +43,6 @@ def playbook_launch_error(reason: str, status_code: int = status.HTTP_400_BAD_RE
...
@@ -44,56 +43,6 @@ def playbook_launch_error(reason: str, status_code: int = status.HTTP_400_BAD_RE
return
JSONResponse
(
content
=
{
"
error
"
:
reason
},
status_code
=
status_code
)
return
JSONResponse
(
content
=
{
"
error
"
:
reason
},
status_code
=
status_code
)
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 :class:`ansible_runner.Runner` runner: Ansible runner that has already executed a playbook.
:return: A filtered dictionary that contains only the relevant parts of the output from executing an Ansible
playbook.
:rtype: list[dict[Any, Any]]
"""
config_params
=
config
.
load
()
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
(
runner
.
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 all
# steps should be included, including those that didn't make changes.
task_result
=
task_output
[
"
event_data
"
][
"
res
"
]
# Remove redundant ansible-related keys.
for
remove
in
config_params
.
filtered_ansible_keys
:
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
and
(
task_result
[
"
state
"
]
==
"
directory
"
or
task_result
[
"
state
"
]
==
"
file
"
):
continue
if
"
diff_lines
"
in
task_result
:
# Juniper-specific
# Prevent the diff from being displayed twice, and only keep the formatted version.
task_result
.
pop
(
"
diff
"
,
None
)
if
bool
(
task_result
):
# Only add the event if there are any relevant keys left.
parsed_output
.
append
({
"
host
"
:
task_output
[
"
event_data
"
][
"
host
"
]}
|
task_result
)
elif
"
ok
"
in
task_output
[
"
event_data
"
]:
# Always include the final message that contains the playbook execution overview.
parsed_output
.
append
(
task_output
[
"
event_data
"
])
except
json
.
JSONDecodeError
:
# 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
(
def
_run_playbook_proc
(
job_id
:
str
,
job_id
:
str
,
playbook_path
:
str
,
playbook_path
:
str
,
...
@@ -115,17 +64,15 @@ def _run_playbook_proc(
...
@@ -115,17 +64,15 @@ def _run_playbook_proc(
extravars
=
extra_vars
,
extravars
=
extra_vars
,
)
)
parsed_output
=
_process_json_output
(
ansible_playbook_run
)
payload
=
{
payload
=
{
"
status
"
:
ansible_playbook_run
.
status
,
"
status
"
:
ansible_playbook_run
.
status
,
"
job_id
"
:
job_id
,
"
job_id
"
:
job_id
,
"
output
"
:
parsed_output
,
"
output
"
:
ansible_playbook_run
.
stdout
.
readlines
()
,
"
return_code
"
:
int
(
ansible_playbook_run
.
rc
),
"
return_code
"
:
int
(
ansible_playbook_run
.
rc
),
}
}
request_result
=
requests
.
post
(
callback
,
json
=
payload
,
timeout
=
DEFAULT_REQUEST_TIMEOUT
)
request_result
=
requests
.
post
(
callback
,
json
=
payload
,
timeout
=
DEFAULT_REQUEST_TIMEOUT
)
if
request_result
.
status_code
!=
status
.
HTTP_
2
00_
OK
:
if
not
status
.
HTTP_200_OK
<=
request_result
.
status_code
<
status
.
HTTP_
3
00_
MULTIPLE_CHOICES
:
msg
=
f
"
Callback failed:
{
request_result
.
text
}
"
msg
=
f
"
Callback failed:
{
request_result
.
text
}
"
logger
.
error
(
msg
)
logger
.
error
(
msg
)
...
...
This diff is collapsed.
Click to expand it.
test/conftest.py
+
1
−
4
View file @
447f301d
...
@@ -39,10 +39,7 @@ def configuration_data() -> dict[str, str]:
...
@@ -39,10 +39,7 @@ def configuration_data() -> dict[str, str]:
(
Path
(
tempdir
)
/
"
iptrunks_checks.yaml
"
).
touch
()
(
Path
(
tempdir
)
/
"
iptrunks_checks.yaml
"
).
touch
()
(
Path
(
tempdir
)
/
"
iptrunks_migration.yaml
"
).
touch
()
(
Path
(
tempdir
)
/
"
iptrunks_migration.yaml
"
).
touch
()
yield
{
yield
{
"
ansible_playbooks_root_dir
"
:
tempdir
}
"
ansible_playbooks_root_dir
"
:
tempdir
,
"
filtered_ansible_keys
"
:
[
"
too_verbose_output_key
"
],
}
@pytest.fixture
(
scope
=
"
session
"
)
@pytest.fixture
(
scope
=
"
session
"
)
...
...
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