2014-11-14 23:14:08 +01:00
|
|
|
#!/usr/bin/env python
|
2015-04-02 20:38:37 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2014-11-14 23:14:08 +01: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 06:19:07 +01:00
|
|
|
from ansible.utils.cli import base_parser, validate_conflicts, normalize_become_options, ask_passwords
|
2015-04-03 19:17:01 +02:00
|
|
|
from ansible.utils.display import Display
|
2015-02-02 22:58:21 +01:00
|
|
|
from ansible.utils.unicode import to_unicode
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.utils.vars import combine_vars
|
2015-03-12 18:14:57 +01:00
|
|
|
from ansible.utils.vault import read_vault_file
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.vars import VariableManager
|
2015-01-19 22:39:45 +01:00
|
|
|
|
|
|
|
# Implement an ansible.utils.warning() function later
|
2015-04-02 20:09:44 +02:00
|
|
|
def warning(*args, **kwargs):
|
|
|
|
print(*args, **kwargs)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
''' run ansible-playbook operations '''
|
|
|
|
|
|
|
|
# create parser for CLI options
|
|
|
|
parser = base_parser(
|
|
|
|
usage = "%prog playbook.yml",
|
|
|
|
connect_opts=True,
|
2015-03-21 05:35:56 +01:00
|
|
|
meta_opts=True,
|
2014-11-14 23:14:08 +01:00
|
|
|
runas_opts=True,
|
|
|
|
subset_opts=True,
|
|
|
|
check_opts=True,
|
2015-03-21 05:35:56 +01:00
|
|
|
diff_opts=True,
|
2014-11-14 23:14:08 +01:00
|
|
|
)
|
2015-03-21 05:35:56 +01:00
|
|
|
|
|
|
|
# ansible playbook specific opts
|
2014-11-14 23:14:08 +01: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 05:35:56 +01:00
|
|
|
parser.add_option('--list-tags', dest='listtags', action='store_true',
|
|
|
|
help="list all available tags")
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
options, args = parser.parse_args(args)
|
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
parser.print_help(file=sys.stderr)
|
|
|
|
return 1
|
|
|
|
|
2015-03-21 05:35:56 +01:00
|
|
|
validate_conflicts(parser,options)
|
|
|
|
|
2015-04-03 10:51:16 +02:00
|
|
|
# Note: slightly wrong, this is written so that implicit localhost
|
2015-03-21 06:19:07 +01:00
|
|
|
# Manage passwords
|
|
|
|
sshpass = None
|
|
|
|
becomepass = None
|
2015-03-12 18:14:57 +01:00
|
|
|
vault_pass = None
|
2015-03-21 06:19:07 +01:00
|
|
|
|
2015-04-03 10:51:16 +02: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-03-21 06:19:07 +01:00
|
|
|
|
|
|
|
if options.vault_password_file:
|
2015-03-12 18:14:57 +01:00
|
|
|
# read vault_pass from a file
|
|
|
|
vault_pass = read_vault_file(options.vault_password_file)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
loader = DataLoader(vault_password=vault_pass)
|
|
|
|
|
|
|
|
extra_vars = {}
|
|
|
|
for extra_vars_opt in options.extra_vars:
|
2015-02-02 23:14:18 +01:00
|
|
|
extra_vars_opt = to_unicode(extra_vars_opt, errors='strict')
|
2015-02-02 22:58:21 +01:00
|
|
|
if extra_vars_opt.startswith(u"@"):
|
2014-11-14 23:14:08 +01:00
|
|
|
# Argument is a YAML file (JSON is a subset of YAML)
|
|
|
|
data = loader.load_from_file(extra_vars_opt[1:])
|
2015-02-02 22:58:21 +01:00
|
|
|
elif extra_vars_opt and extra_vars_opt[0] in u'[{':
|
2014-11-14 23:14:08 +01:00
|
|
|
# Arguments as YAML
|
2015-01-27 06:41:02 +01:00
|
|
|
data = loader.load(extra_vars_opt)
|
2014-11-14 23:14:08 +01:00
|
|
|
else:
|
|
|
|
# Arguments as Key-value
|
|
|
|
data = parse_kv(extra_vars_opt)
|
2015-01-27 06:41:02 +01:00
|
|
|
extra_vars = combine_vars(extra_vars, data)
|
2014-11-14 23:14:08 +01: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 04:45:25 +01:00
|
|
|
variable_manager.set_inventory(inventory)
|
2015-01-19 22:39:45 +01: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 23:14:08 +01:00
|
|
|
if len(inventory.list_hosts()) == 0:
|
2015-01-19 22:39:45 +01:00
|
|
|
# Empty inventory
|
2015-01-17 00:31:58 +01:00
|
|
|
warning("provided hosts list is empty, only localhost is available")
|
2015-01-19 22:39:45 +01: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 23:14:08 +01:00
|
|
|
|
|
|
|
# create the playbook executor, which manages running the plays
|
|
|
|
# via a task queue manager
|
2015-04-03 19:17:01 +02:00
|
|
|
display = Display()
|
|
|
|
pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, display=display, options=options)
|
2015-04-03 10:51:16 +02:00
|
|
|
|
|
|
|
if options.listhosts:
|
2015-04-03 19:02:42 +02:00
|
|
|
i = 1
|
|
|
|
for play in pbex.listhosts():
|
|
|
|
print("\nplay #%d" % i)
|
|
|
|
for host in sorted(play):
|
|
|
|
print(" %s" % host)
|
|
|
|
i = i + 1
|
2015-04-03 10:51:16 +02:00
|
|
|
sys.exit(0)
|
|
|
|
elif options.listtasks:
|
|
|
|
print('TODO: implement')
|
|
|
|
sys.exit(0)
|
|
|
|
elif options.listtags:
|
|
|
|
print('TODO: implement')
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
return pbex.run()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
#display(" ", log_only=True)
|
|
|
|
#display(" ".join(sys.argv), log_only=True)
|
|
|
|
#display(" ", log_only=True)
|
|
|
|
try:
|
|
|
|
sys.exit(main(sys.argv[1:]))
|
2015-04-02 20:38:37 +02:00
|
|
|
except AnsibleError as e:
|
2014-11-14 23:14:08 +01:00
|
|
|
#display("ERROR: %s" % e, color='red', stderr=True)
|
2015-04-02 20:09:44 +02:00
|
|
|
print(e)
|
2014-11-14 23:14:08 +01:00
|
|
|
sys.exit(1)
|
2015-04-02 20:38:37 +02:00
|
|
|
except KeyboardInterrupt:
|
2014-11-14 23:14:08 +01:00
|
|
|
#display("ERROR: interrupted", color='red', stderr=True)
|
2015-04-02 20:09:44 +02:00
|
|
|
print("keyboard interrupt")
|
2014-11-14 23:14:08 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|