Skip to content

Commit

Permalink
using ansible logging (#156)
Browse files Browse the repository at this point in the history
* using ansible logging

* added help for code debugging
  • Loading branch information
EarthmanT authored Dec 12, 2022
1 parent 310e5fa commit 3d858f1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,4 @@
3.1.0: RD-5392 Kerberos Support
3.1.1: Add missing configure task
3.1.2: Ignore system default for ansible because Cloudify requires python 3.
3.1.3: Extract error messages from hosts if possible.
60 changes: 59 additions & 1 deletion cloudify_ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ansible.playbook import Playbook
from cloudify.manager import get_rest_client
from cloudify.utils import LocalCommandRunner
from script_runner.tasks import ProcessException
from ansible.vars.manager import VariableManager
from ansible.parsing.dataloader import DataLoader
from cloudify_rest_client.constants import VisibilityState
Expand Down Expand Up @@ -1016,7 +1017,15 @@ def returns(value):

ctx._return_value = None

actual_result = script_func(script_path, ctx, process)
try:
actual_result = script_func(script_path, ctx, process)
except ProcessException as e:
issues = get_issues_from_process_exception(e)
if issues:
raise NonRecoverableError(
'The following Ansible tasks failed: {}'.format(issues))
else:
raise e
script_result = ctx._return_value
if ctx.is_script_exception_defined and isinstance(
script_result, ScriptException):
Expand All @@ -1028,6 +1037,55 @@ def returns(value):
return actual_result


def get_message_from_failed_task(task, host):
try:
if task['hosts'][host]['failed']:
message = 'The action {} for host {} failed: {}'.format(
{task['hosts'][host]['action']},
host,
{task['hosts'][host]['msg']})
return message
except KeyError:
return


def get_issues_from_process_exception(exc):
try:
# We have a big stdout message with JSON result. After META...
result = exc.stdout.split('META: ran handlers')[-1]
loaded_result = json.loads(result)
issues = []
# Example Result:
# {
# "plays": [
# {
# "play": {"name": "localhost"},
# "tasks": [
# {
# "hosts": {
# "localhost": {
# "action": "debug",
# "changed": false,
# "failed": true,
# "msg": "Invalid options for debug"
# }
# },
# }
# ]
# }
# ]
# }
for play in loaded_result['plays']:
for task in play['tasks']:
for host in task['hosts']:
message = get_message_from_failed_task(task, host)
if message:
issues.append(message)
return issues
except Exception:
return


def get_plays(string, node_name):
"""When play output is returned in JSON, we can parse it and
retrieve only the play dictionary. This is a little messy, and it's
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
ansible:
executor: central_deployment_agent
package_name: cloudify-ansible-plugin
package_version: '3.1.2'
package_version: '3.1.3'

dsl_definitions:

Expand Down
2 changes: 1 addition & 1 deletion plugin_1_4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
ansible:
executor: central_deployment_agent
package_name: cloudify-ansible-plugin
package_version: '3.1.2'
package_version: '3.1.3'

dsl_definitions:

Expand Down
2 changes: 1 addition & 1 deletion v2_plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
ansible:
executor: central_deployment_agent
package_name: cloudify-ansible-plugin
package_version: '3.1.2'
package_version: '3.1.3'

dsl_definitions:

Expand Down

0 comments on commit 3d858f1

Please sign in to comment.