ansible-connection verboistery (#77509)

* ansible-connection verboistery

  for cli, just use normal parser creation
 this also adds --help, but that seems fine
 also some error cleanup

Co-authored-by: Nathaniel Case <this.is@nathanielca.se>
pull/77544/head
Brian Coca 2 years ago committed by GitHub
parent 3b9592fcaf
commit a0dede5458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- ansible-connection now supports verbosity directly on cli

@ -23,7 +23,7 @@ import json
from contextlib import contextmanager
from ansible import constants as C
from ansible.cli.arguments.option_helpers import AnsibleVersion
from ansible.cli.arguments import option_helpers as opt_help
from ansible.module_utils._text import to_bytes, to_text
from ansible.module_utils.connection import Connection, ConnectionError, send_data, recv_data
from ansible.module_utils.service import fork_process
@ -224,12 +224,16 @@ class ConnectionProcess(object):
def main(args=None):
""" Called to initiate the connect to the remote device
"""
parser = argparse.ArgumentParser(prog='ansible-connection', add_help=False)
parser.add_argument('--version', action=AnsibleVersion, nargs=0)
parser = opt_help.create_base_parser(prog='ansible-connection')
opt_help.add_verbosity_options(parser)
parser.add_argument('playbook_pid')
parser.add_argument('task_uuid')
args = parser.parse_args(args[1:] if args is not None else args)
# initialize verbosity
display.verbosity = args.verbosity
rc = 0
result = {}
messages = list()
@ -252,7 +256,6 @@ def main(args=None):
play_context = PlayContext()
play_context.deserialize(pc_data)
display.verbosity = play_context.verbosity
except Exception as e:
rc = 1

@ -189,8 +189,8 @@ class TaskExecutor:
except AnsibleError as e:
return dict(failed=True, msg=wrap_var(to_text(e, nonstring='simplerepr')), _ansible_no_log=self._play_context.no_log)
except Exception as e:
return dict(failed=True, msg='Unexpected failure during module execution.', exception=to_text(traceback.format_exc()),
stdout='', _ansible_no_log=self._play_context.no_log)
return dict(failed=True, msg=wrap_var('Unexpected failure during module execution: %s' % (to_native(e, nonstring='simplerepr'))),
exception=to_text(traceback.format_exc()), stdout='', _ansible_no_log=self._play_context.no_log)
finally:
try:
self._connection.close()
@ -521,7 +521,7 @@ class TaskExecutor:
# Now we do final validation on the task, which sets all fields to their final values.
try:
self._task.post_validate(templar=templar)
except AnsibleError as e:
except AnsibleError:
raise
except Exception:
return dict(changed=False, failed=True, _ansible_no_log=no_log, exception=to_text(traceback.format_exc()))
@ -1165,10 +1165,13 @@ def start_connection(play_context, variables, task_uuid):
'ANSIBLE_NETCONF_PLUGINS': netconf_loader.print_paths(),
'ANSIBLE_TERMINAL_PLUGINS': terminal_loader.print_paths(),
})
verbosity = []
if display.verbosity:
verbosity.append('-%s' % ('v' * display.verbosity))
python = sys.executable
master, slave = pty.openpty()
p = subprocess.Popen(
[python, ansible_connection, to_text(os.getppid()), to_text(task_uuid)],
[python, ansible_connection, *verbosity, to_text(os.getppid()), to_text(task_uuid)],
stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
)
os.close(slave)

Loading…
Cancel
Save