2014-11-14 22:14:08 +00:00
|
|
|
#!/usr/bin/env python
|
2015-04-02 18:38:37 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from ansible import constants as C
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.executor.playbook_executor import PlaybookExecutor
|
|
|
|
from ansible.inventory import Inventory
|
|
|
|
from ansible.parsing import DataLoader
|
|
|
|
from ansible.parsing.splitter import parse_kv
|
|
|
|
from ansible.playbook import Playbook
|
|
|
|
from ansible.playbook.task import Task
|
2015-03-21 05:19:07 +00:00
|
|
|
from ansible.utils.cli import base_parser, validate_conflicts, normalize_become_options, ask_passwords
|
2015-04-03 17:17:01 +00:00
|
|
|
from ansible.utils.display import Display
|
2015-02-02 21:58:21 +00:00
|
|
|
from ansible.utils.unicode import to_unicode
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.utils.vars import combine_vars
|
2015-03-12 17:14:57 +00:00
|
|
|
from ansible.utils.vault import read_vault_file
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.vars import VariableManager
|
2015-01-19 21:39:45 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
#---------------------------------------------------------------------------------------------------
|
|
|
|
|
2015-04-04 19:14:40 +00:00
|
|
|
def main(display, args):
|
2014-11-14 22:14:08 +00:00
|
|
|
''' run ansible-playbook operations '''
|
|
|
|
|
|
|
|
# create parser for CLI options
|
|
|
|
parser = base_parser(
|
|
|
|
usage = "%prog playbook.yml",
|
|
|
|
connect_opts=True,
|
2015-03-21 04:35:56 +00:00
|
|
|
meta_opts=True,
|
2014-11-14 22:14:08 +00:00
|
|
|
runas_opts=True,
|
|
|
|
subset_opts=True,
|
|
|
|
check_opts=True,
|
2015-03-21 04:35:56 +00:00
|
|
|
diff_opts=True,
|
2014-11-14 22:14:08 +00:00
|
|
|
)
|
2015-03-21 04:35:56 +00:00
|
|
|
|
|
|
|
# ansible playbook specific opts
|
2014-11-14 22:14:08 +00:00
|
|
|
parser.add_option('--list-tasks', dest='listtasks', action='store_true',
|
|
|
|
help="list all tasks that would be executed")
|
|
|
|
parser.add_option('--step', dest='step', action='store_true',
|
|
|
|
help="one-step-at-a-time: confirm each task before running")
|
|
|
|
parser.add_option('--start-at-task', dest='start_at',
|
|
|
|
help="start the playbook at the task matching this name")
|
2015-03-21 04:35:56 +00:00
|
|
|
parser.add_option('--list-tags', dest='listtags', action='store_true',
|
|
|
|
help="list all available tags")
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
options, args = parser.parse_args(args)
|
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
parser.print_help(file=sys.stderr)
|
|
|
|
return 1
|
|
|
|
|
2015-04-04 19:54:54 +00:00
|
|
|
display.verbosity = options.verbosity
|
2015-03-21 04:35:56 +00:00
|
|
|
validate_conflicts(parser,options)
|
|
|
|
|
2015-04-03 08:51:16 +00:00
|
|
|
# Note: slightly wrong, this is written so that implicit localhost
|
2015-03-21 05:19:07 +00:00
|
|
|
# Manage passwords
|
|
|
|
sshpass = None
|
|
|
|
becomepass = None
|
2015-03-12 17:14:57 +00:00
|
|
|
vault_pass = None
|
2015-03-21 05:19:07 +00:00
|
|
|
|
2015-04-03 08:51:16 +00:00
|
|
|
# don't deal with privilege escalation when we don't need to
|
|
|
|
if not options.listhosts and not options.listtasks and not options.listtags:
|
|
|
|
normalize_become_options(options)
|
|
|
|
(sshpass, becomepass, vault_pass) = ask_passwords(options)
|
2015-04-08 07:16:13 +00:00
|
|
|
passwords = { 'conn_pass': sshpass, 'become_pass': becomepass }
|
2015-03-21 05:19:07 +00:00
|
|
|
|
|
|
|
if options.vault_password_file:
|
2015-03-12 17:14:57 +00:00
|
|
|
# read vault_pass from a file
|
|
|
|
vault_pass = read_vault_file(options.vault_password_file)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
loader = DataLoader(vault_password=vault_pass)
|
|
|
|
|
|
|
|
extra_vars = {}
|
|
|
|
for extra_vars_opt in options.extra_vars:
|
2015-02-02 22:14:18 +00:00
|
|
|
extra_vars_opt = to_unicode(extra_vars_opt, errors='strict')
|
2015-02-02 21:58:21 +00:00
|
|
|
if extra_vars_opt.startswith(u"@"):
|
2014-11-14 22:14:08 +00:00
|
|
|
# Argument is a YAML file (JSON is a subset of YAML)
|
|
|
|
data = loader.load_from_file(extra_vars_opt[1:])
|
2015-02-02 21:58:21 +00:00
|
|
|
elif extra_vars_opt and extra_vars_opt[0] in u'[{':
|
2014-11-14 22:14:08 +00:00
|
|
|
# Arguments as YAML
|
2015-01-27 05:41:02 +00:00
|
|
|
data = loader.load(extra_vars_opt)
|
2014-11-14 22:14:08 +00:00
|
|
|
else:
|
|
|
|
# Arguments as Key-value
|
|
|
|
data = parse_kv(extra_vars_opt)
|
2015-01-27 05:41:02 +00:00
|
|
|
extra_vars = combine_vars(extra_vars, data)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
# FIXME: this should be moved inside the playbook executor code
|
|
|
|
only_tags = options.tags.split(",")
|
|
|
|
skip_tags = options.skip_tags
|
|
|
|
if options.skip_tags is not None:
|
|
|
|
skip_tags = options.skip_tags.split(",")
|
|
|
|
|
|
|
|
# initial error check, to make sure all specified playbooks are accessible
|
|
|
|
# before we start running anything through the playbook executor
|
|
|
|
for playbook in args:
|
|
|
|
if not os.path.exists(playbook):
|
|
|
|
raise AnsibleError("the playbook: %s could not be found" % playbook)
|
|
|
|
if not (os.path.isfile(playbook) or stat.S_ISFIFO(os.stat(playbook).st_mode)):
|
|
|
|
raise AnsibleError("the playbook: %s does not appear to be a file" % playbook)
|
|
|
|
|
|
|
|
# create the variable manager, which will be shared throughout
|
|
|
|
# the code, ensuring a consistent view of global variables
|
|
|
|
variable_manager = VariableManager()
|
|
|
|
variable_manager.set_extra_vars(extra_vars)
|
|
|
|
|
|
|
|
# create the inventory, and filter it based on the subset specified (if any)
|
|
|
|
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=options.inventory)
|
2015-01-23 03:45:25 +00:00
|
|
|
variable_manager.set_inventory(inventory)
|
2015-01-19 21:39:45 +00:00
|
|
|
|
|
|
|
# (which is not returned in list_hosts()) is taken into account for
|
|
|
|
# warning if inventory is empty. But it can't be taken into account for
|
|
|
|
# checking if limit doesn't match any hosts. Instead we don't worry about
|
|
|
|
# limit if only implicit localhost was in inventory to start with.
|
|
|
|
#
|
|
|
|
# Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts())
|
|
|
|
no_hosts = False
|
2014-11-14 22:14:08 +00:00
|
|
|
if len(inventory.list_hosts()) == 0:
|
2015-01-19 21:39:45 +00:00
|
|
|
# Empty inventory
|
2015-04-04 19:14:40 +00:00
|
|
|
display.warning("provided hosts list is empty, only localhost is available")
|
2015-01-19 21:39:45 +00:00
|
|
|
no_hosts = True
|
|
|
|
inventory.subset(options.subset)
|
|
|
|
if len(inventory.list_hosts()) == 0 and no_hosts is False:
|
|
|
|
# Invalid limit
|
|
|
|
raise errors.AnsibleError("Specified --limit does not match any hosts")
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-04-04 19:14:40 +00:00
|
|
|
# create the playbook executor, which manages running the plays via a task queue manager
|
2015-04-08 07:16:13 +00:00
|
|
|
pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, display=display, options=options, passwords=passwords)
|
2015-04-03 08:51:16 +00:00
|
|
|
|
2015-04-04 14:25:55 +00:00
|
|
|
results = pbex.run()
|
|
|
|
|
|
|
|
if isinstance(results, list):
|
|
|
|
for p in results:
|
|
|
|
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display('\nplaybook: %s\n' % p['playbook'])
|
2015-04-04 14:25:55 +00:00
|
|
|
for play in p['plays']:
|
|
|
|
if options.listhosts:
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display("\n %s (%s): host count=%d" % (play['name'], play['pattern'], len(play['hosts'])))
|
2015-04-04 14:25:55 +00:00
|
|
|
for host in play['hosts']:
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display(" %s" % host)
|
2015-04-04 14:25:55 +00:00
|
|
|
if options.listtasks: #TODO: do we want to display block info?
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display("\n %s" % (play['name']))
|
2015-04-04 14:25:55 +00:00
|
|
|
for task in play['tasks']:
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display(" %s" % task)
|
|
|
|
if options.listtags: #TODO: fix once we figure out block handling above
|
|
|
|
display.display("\n %s: tags count=%d" % (play['name'], len(play['tags'])))
|
2015-04-04 14:25:55 +00:00
|
|
|
for tag in play['tags']:
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display(" %s" % tag)
|
|
|
|
return 0
|
2015-04-03 08:51:16 +00:00
|
|
|
else:
|
2015-04-04 14:25:55 +00:00
|
|
|
return results
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-04-04 19:14:40 +00:00
|
|
|
|
|
|
|
display = Display()
|
2015-04-04 19:54:54 +00:00
|
|
|
#display.display(" ".join(sys.argv), log_only=True)
|
2015-04-04 19:14:40 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
try:
|
2015-04-04 19:14:40 +00:00
|
|
|
sys.exit(main(display, sys.argv[1:]))
|
2015-04-02 18:38:37 +00:00
|
|
|
except AnsibleError as e:
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display("[ERROR]: %s" % e, color='red', stderr=True)
|
2014-11-14 22:14:08 +00:00
|
|
|
sys.exit(1)
|
2015-04-02 18:38:37 +00:00
|
|
|
except KeyboardInterrupt:
|
2015-04-04 19:14:40 +00:00
|
|
|
display.display("[ERROR]: interrupted", color='red', stderr=True)
|
2014-11-14 22:14:08 +00:00
|
|
|
sys.exit(1)
|