updated ansible-playbook to use display, fixed issues breaking display class

This commit is contained in:
Brian Coca 2015-04-04 15:14:40 -04:00
parent e6e69c0894
commit af97e732a0
4 changed files with 25 additions and 34 deletions

View file

@ -16,7 +16,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish # Make coding more python3-ish
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division)
__metaclass__ = type __metaclass__ = type
import signal import signal

View file

@ -16,7 +16,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish # Make coding more python3-ish
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division)
__metaclass__ = type __metaclass__ = type
from ansible.errors import AnsibleError, AnsibleParserError from ansible.errors import AnsibleError, AnsibleParserError

View file

@ -18,6 +18,7 @@
# FIXME: copied mostly from old code, needs py3 improvements # FIXME: copied mostly from old code, needs py3 improvements
import textwrap import textwrap
import sys
from ansible import constants as C from ansible import constants as C
from ansible.errors import * from ansible.errors import *
@ -97,15 +98,15 @@ class Display:
new_msg = "\n".join(wrapped) + "\n" new_msg = "\n".join(wrapped) + "\n"
if new_msg not in deprecations: if new_msg not in deprecations:
self._display(new_msg, color='purple', stderr=True) self.display(new_msg, color='purple', stderr=True)
self._deprecations[new_msg] = 1 self._deprecations[new_msg] = 1
def warning(self, msg): def warning(self, msg):
new_msg = "\n[WARNING]: %s" % msg new_msg = "\n[WARNING]: %s" % msg
wrapped = textwrap.wrap(new_msg, 79) wrapped = textwrap.wrap(new_msg, 79)
new_msg = "\n".join(wrapped) + "\n" new_msg = "\n".join(wrapped) + "\n"
if new_msg not in warns: if new_msg not in self._warns:
self._display(new_msg, color='bright purple', stderr=True) self.display(new_msg, color='bright purple', stderr=True)
self._warns[new_msg] = 1 self._warns[new_msg] = 1
def system_warning(self, msg): def system_warning(self, msg):

View file

@ -21,13 +21,9 @@ from ansible.utils.vars import combine_vars
from ansible.utils.vault import read_vault_file from ansible.utils.vault import read_vault_file
from ansible.vars import VariableManager from ansible.vars import VariableManager
# Implement an ansible.utils.warning() function later
def warning(*args, **kwargs):
print(*args, **kwargs)
#--------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------
def main(args): def main(display, args):
''' run ansible-playbook operations ''' ''' run ansible-playbook operations '''
# create parser for CLI options # create parser for CLI options
@ -122,16 +118,14 @@ def main(args):
no_hosts = False no_hosts = False
if len(inventory.list_hosts()) == 0: if len(inventory.list_hosts()) == 0:
# Empty inventory # Empty inventory
warning("provided hosts list is empty, only localhost is available") display.warning("provided hosts list is empty, only localhost is available")
no_hosts = True no_hosts = True
inventory.subset(options.subset) inventory.subset(options.subset)
if len(inventory.list_hosts()) == 0 and no_hosts is False: if len(inventory.list_hosts()) == 0 and no_hosts is False:
# Invalid limit # Invalid limit
raise errors.AnsibleError("Specified --limit does not match any hosts") raise errors.AnsibleError("Specified --limit does not match any hosts")
# create the playbook executor, which manages running the plays # create the playbook executor, which manages running the plays via a task queue manager
# via a task queue manager
display = Display()
pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, display=display, options=options) pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, display=display, options=options)
results = pbex.run() results = pbex.run()
@ -139,38 +133,34 @@ def main(args):
if isinstance(results, list): if isinstance(results, list):
for p in results: for p in results:
print('') display.display('\nplaybook: %s\n' % p['playbook'])
print('playbook: %s' % p['playbook'])
print('')
for play in p['plays']: for play in p['plays']:
if options.listhosts: if options.listhosts:
print("\n %s (%s): host count=%d" % (play['name'], play['pattern'], len(play['hosts']))) display.display("\n %s (%s): host count=%d" % (play['name'], play['pattern'], len(play['hosts'])))
for host in play['hosts']: for host in play['hosts']:
print(" %s" % host) display.display(" %s" % host)
if options.listtasks: #TODO: do we want to display block info? if options.listtasks: #TODO: do we want to display block info?
print("\n %s: task count=%d" % (play['name'], len(play['tasks']))) display.display("\n %s" % (play['name']))
for task in play['tasks']: for task in play['tasks']:
print(" %s" % task) display.display(" %s" % task)
if options.listtags: if options.listtags: #TODO: fix once we figure out block handling above
print("\n %s: tags count=%d" % (play['name'], len(play['tags']))) display.display("\n %s: tags count=%d" % (play['name'], len(play['tags'])))
for tag in play['tags']: for tag in play['tags']:
print(" %s" % tag) display.display(" %s" % tag)
return 0
else: else:
return results return results
if __name__ == "__main__": if __name__ == "__main__":
#display(" ", log_only=True)
#display(" ".join(sys.argv), log_only=True) display = Display()
#display(" ", log_only=True) display.display(" ".join(sys.argv), log_only=True)
try: try:
sys.exit(main(sys.argv[1:])) sys.exit(main(display, sys.argv[1:]))
except AnsibleError as e: except AnsibleError as e:
#display("ERROR: %s" % e, color='red', stderr=True) display.display("[ERROR]: %s" % e, color='red', stderr=True)
print(e)
sys.exit(1) sys.exit(1)
except KeyboardInterrupt: except KeyboardInterrupt:
#display("ERROR: interrupted", color='red', stderr=True) display.display("[ERROR]: interrupted", color='red', stderr=True)
print("keyboard interrupt")
sys.exit(1) sys.exit(1)