2018-12-18 02:10:59 +00:00
|
|
|
# Copyright: (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
# Copyright: (c) 2018, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2015-05-01 01:22:23 +00:00
|
|
|
|
2015-10-20 01:36:19 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-05-01 22:29:15 +00:00
|
|
|
import datetime
|
2015-05-01 01:22:23 +00:00
|
|
|
import os
|
2015-08-12 14:24:34 +00:00
|
|
|
import platform
|
2015-05-01 22:29:15 +00:00
|
|
|
import random
|
|
|
|
import shutil
|
|
|
|
import socket
|
2015-06-09 21:24:06 +00:00
|
|
|
import sys
|
2015-08-12 14:26:22 +00:00
|
|
|
import time
|
2015-05-01 01:22:23 +00:00
|
|
|
|
2018-08-28 19:15:14 +00:00
|
|
|
from ansible import constants as C
|
2018-12-18 02:10:59 +00:00
|
|
|
from ansible import context
|
|
|
|
from ansible.cli import CLI
|
2018-12-20 18:07:49 +00:00
|
|
|
from ansible.cli.arguments import optparse_helpers as opt_help
|
2017-06-02 11:14:11 +00:00
|
|
|
from ansible.errors import AnsibleOptionsError
|
2018-03-19 16:10:50 +00:00
|
|
|
from ansible.module_utils._text import to_native, to_text
|
2019-01-23 15:58:37 +00:00
|
|
|
from ansible.module_utils.six.moves import shlex_quote
|
2017-08-15 20:38:59 +00:00
|
|
|
from ansible.plugins.loader import module_loader
|
2015-06-09 21:24:06 +00:00
|
|
|
from ansible.utils.cmd_functions import run_cmd
|
2018-11-20 23:06:51 +00:00
|
|
|
from ansible.utils.display import Display
|
2015-05-01 01:22:23 +00:00
|
|
|
|
2018-11-20 23:06:51 +00:00
|
|
|
display = Display()
|
2015-11-10 19:40:55 +00:00
|
|
|
|
|
|
|
|
2015-05-01 01:22:23 +00:00
|
|
|
class PullCLI(CLI):
|
2017-03-23 05:11:40 +00:00
|
|
|
''' is used to up a remote copy of ansible on each managed node,
|
|
|
|
each set to run via cron and update playbook source via a source repository.
|
|
|
|
This inverts the default *push* architecture of ansible into a *pull* architecture,
|
|
|
|
which has near-limitless scaling potential.
|
|
|
|
|
|
|
|
The setup playbook can be tuned to change the cron frequency, logging locations, and parameters to ansible-pull.
|
|
|
|
This is useful both for extreme scale-out as well as periodic remediation.
|
|
|
|
Usage of the 'fetch' module to retrieve logs from ansible-pull runs would be an
|
|
|
|
excellent way to gather and analyze remote logs from ansible-pull.
|
|
|
|
'''
|
2015-05-01 01:22:23 +00:00
|
|
|
|
2015-05-01 22:29:15 +00:00
|
|
|
DEFAULT_REPO_TYPE = 'git'
|
|
|
|
DEFAULT_PLAYBOOK = 'local.yml'
|
2017-12-14 03:16:54 +00:00
|
|
|
REPO_CHOICES = ('git', 'subversion', 'hg', 'bzr')
|
2015-05-01 22:29:15 +00:00
|
|
|
PLAYBOOK_ERRORS = {
|
|
|
|
1: 'File does not exist',
|
2017-06-02 11:14:11 +00:00
|
|
|
2: 'File is not readable',
|
2015-05-01 22:29:15 +00:00
|
|
|
}
|
|
|
|
SUPPORTED_REPO_MODULES = ['git']
|
2017-06-02 11:14:11 +00:00
|
|
|
ARGUMENTS = {'playbook.yml': 'The name of one the YAML format files to run as an Ansible playbook.'
|
2017-03-23 05:11:40 +00:00
|
|
|
'This can be a relative path within the checkout. By default, Ansible will'
|
|
|
|
"look for a playbook based on the host's fully-qualified domain name,"
|
2017-03-24 16:38:16 +00:00
|
|
|
'on the host hostname and finally a playbook named *local.yml*.', }
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2017-10-25 23:55:48 +00:00
|
|
|
SKIP_INVENTORY_DEFAULTS = True
|
|
|
|
|
2018-12-20 04:45:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_inv_cli():
|
2017-09-27 23:50:36 +00:00
|
|
|
inv_opts = ''
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS.get('inventory', False):
|
|
|
|
for inv in context.CLIARGS['inventory']:
|
2017-09-27 23:50:36 +00:00
|
|
|
if isinstance(inv, list):
|
|
|
|
inv_opts += " -i '%s' " % ','.join(inv)
|
|
|
|
elif ',' in inv or os.path.exists(inv):
|
|
|
|
inv_opts += ' -i %s ' % inv
|
|
|
|
|
|
|
|
return inv_opts
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
def init_parser(self):
|
2015-05-01 01:22:23 +00:00
|
|
|
''' create an options parser for bin/ansible '''
|
|
|
|
|
2018-12-19 08:28:33 +00:00
|
|
|
super(PullCLI, self).init_parser(
|
2017-03-23 05:11:40 +00:00
|
|
|
usage='%prog -U <repository> [options] [<playbook.yml>]',
|
2018-12-19 08:28:33 +00:00
|
|
|
desc="pulls playbooks from a VCS repo and executes them for the local host")
|
|
|
|
|
|
|
|
# Do not add check_options as there's a conflict with --checkout/-C
|
|
|
|
opt_help.add_connect_options(self.parser)
|
|
|
|
opt_help.add_vault_options(self.parser)
|
|
|
|
opt_help.add_runtask_options(self.parser)
|
|
|
|
opt_help.add_subset_options(self.parser)
|
|
|
|
opt_help.add_inventory_options(self.parser)
|
|
|
|
opt_help.add_module_options(self.parser)
|
|
|
|
opt_help.add_runas_prompt_options(self.parser)
|
2015-05-01 01:22:23 +00:00
|
|
|
|
|
|
|
# options unique to pull
|
2017-03-23 05:11:40 +00:00
|
|
|
self.parser.add_option('--purge', default=False, action='store_true', help='purge checkout after playbook run')
|
2015-05-01 22:29:15 +00:00
|
|
|
self.parser.add_option('-o', '--only-if-changed', dest='ifchanged', default=False, action='store_true',
|
2017-06-02 11:14:11 +00:00
|
|
|
help='only run the playbook if the repository has been updated')
|
2015-05-01 22:29:15 +00:00
|
|
|
self.parser.add_option('-s', '--sleep', dest='sleep', default=None,
|
2017-06-02 11:14:11 +00:00
|
|
|
help='sleep for random interval (between 0 and n number of seconds) before starting. '
|
|
|
|
'This is a useful way to disperse git requests')
|
2015-05-01 22:29:15 +00:00
|
|
|
self.parser.add_option('-f', '--force', dest='force', default=False, action='store_true',
|
2017-06-02 11:14:11 +00:00
|
|
|
help='run the playbook even if the repository could not be updated')
|
2017-03-23 05:11:40 +00:00
|
|
|
self.parser.add_option('-d', '--directory', dest='dest', default=None, help='directory to checkout repository to')
|
|
|
|
self.parser.add_option('-U', '--url', dest='url', default=None, help='URL of the playbook repository')
|
|
|
|
self.parser.add_option('--full', dest='fullclone', action='store_true', help='Do a full clone, instead of a shallow one.')
|
2015-05-01 22:29:15 +00:00
|
|
|
self.parser.add_option('-C', '--checkout', dest='checkout',
|
2017-06-02 11:14:11 +00:00
|
|
|
help='branch/tag/commit to checkout. Defaults to behavior of repository module.')
|
2015-05-01 22:29:15 +00:00
|
|
|
self.parser.add_option('--accept-host-key', default=False, dest='accept_host_key', action='store_true',
|
2017-06-02 11:14:11 +00:00
|
|
|
help='adds the hostkey for the repo url if not already added')
|
2015-05-01 22:29:15 +00:00
|
|
|
self.parser.add_option('-m', '--module-name', dest='module_name', default=self.DEFAULT_REPO_TYPE,
|
2017-12-14 03:16:54 +00:00
|
|
|
help='Repository module name, which ansible will use to check out the repo. Choices are %s. Default is %s.'
|
|
|
|
% (self.REPO_CHOICES, self.DEFAULT_REPO_TYPE))
|
2015-06-10 07:27:25 +00:00
|
|
|
self.parser.add_option('--verify-commit', dest='verify', default=False, action='store_true',
|
2017-06-02 11:14:11 +00:00
|
|
|
help='verify GPG signature of checked out commit, if it fails abort running the playbook. '
|
|
|
|
'This needs the corresponding VCS module to support such an operation')
|
2016-06-23 13:02:34 +00:00
|
|
|
self.parser.add_option('--clean', dest='clean', default=False, action='store_true',
|
2017-06-02 11:14:11 +00:00
|
|
|
help='modified files in the working repository will be discarded')
|
2016-06-23 13:02:34 +00:00
|
|
|
self.parser.add_option('--track-subs', dest='tracksubs', default=False, action='store_true',
|
2017-06-02 11:14:11 +00:00
|
|
|
help='submodules will track the latest changes. This is equivalent to specifying the --remote flag to git submodule update')
|
2018-08-28 19:15:14 +00:00
|
|
|
# add a subset of the check_opts flag group manually, as the full set's
|
|
|
|
# shortcodes conflict with above --checkout/-C
|
2017-08-14 15:20:05 +00:00
|
|
|
self.parser.add_option("--check", default=False, dest='check', action='store_true',
|
|
|
|
help="don't make any changes; instead, try to predict some of the changes that may occur")
|
2018-08-28 19:15:14 +00:00
|
|
|
self.parser.add_option("--diff", default=C.DIFF_ALWAYS, dest='diff', action='store_true',
|
|
|
|
help="when changing (small) files and templates, show the differences in those files; works great with --check")
|
2015-05-01 01:22:23 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
def post_process_args(self, options, args):
|
|
|
|
options, args = super(PullCLI, self).post_process_args(options, args)
|
|
|
|
|
|
|
|
if not options.dest:
|
2015-12-04 03:42:05 +00:00
|
|
|
hostname = socket.getfqdn()
|
|
|
|
# use a hostname dependent directory, in case of $HOME on nfs
|
2018-12-18 02:10:59 +00:00
|
|
|
options.dest = os.path.join('~/.ansible/pull', hostname)
|
|
|
|
options.dest = os.path.expandvars(os.path.expanduser(options.dest))
|
2015-12-04 03:42:05 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if os.path.exists(options.dest) and not os.path.isdir(options.dest):
|
|
|
|
raise AnsibleOptionsError("%s is not a valid or accessible directory." % options.dest)
|
2017-10-09 21:17:51 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if options.sleep:
|
2015-05-01 22:29:15 +00:00
|
|
|
try:
|
2018-12-18 02:10:59 +00:00
|
|
|
secs = random.randint(0, int(options.sleep))
|
|
|
|
options.sleep = secs
|
2015-05-01 22:29:15 +00:00
|
|
|
except ValueError:
|
2018-12-18 02:10:59 +00:00
|
|
|
raise AnsibleOptionsError("%s is not a number." % options.sleep)
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if not options.url:
|
2015-05-01 22:29:15 +00:00
|
|
|
raise AnsibleOptionsError("URL for repository not specified, use -h for help")
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if options.module_name not in self.SUPPORTED_REPO_MODULES:
|
|
|
|
raise AnsibleOptionsError("Unsupported repo module %s, choices are %s" % (options.module_name, ','.join(self.SUPPORTED_REPO_MODULES)))
|
|
|
|
|
|
|
|
display.verbosity = options.verbosity
|
|
|
|
self.validate_conflicts(options, vault_opts=True)
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
return options, args
|
2015-05-01 01:22:23 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
''' use Runner lib to do SSH things '''
|
|
|
|
|
2015-07-04 14:23:30 +00:00
|
|
|
super(PullCLI, self).run()
|
|
|
|
|
2015-05-01 22:29:15 +00:00
|
|
|
# log command line
|
|
|
|
now = datetime.datetime.now()
|
2015-11-10 19:40:55 +00:00
|
|
|
display.display(now.strftime("Starting Ansible Pull at %F %T"))
|
|
|
|
display.display(' '.join(sys.argv))
|
2015-05-01 22:29:15 +00:00
|
|
|
|
|
|
|
# Build Checkout command
|
|
|
|
# Now construct the ansible command
|
2015-08-12 14:24:34 +00:00
|
|
|
node = platform.node()
|
|
|
|
host = socket.getfqdn()
|
2015-10-29 13:02:11 +00:00
|
|
|
limit_opts = 'localhost,%s,127.0.0.1' % ','.join(set([host, node, host.split('.')[0], node.split('.')[0]]))
|
2015-12-04 04:47:02 +00:00
|
|
|
base_opts = '-c local '
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['verbosity'] > 0:
|
|
|
|
base_opts += ' -%s' % ''.join(["v" for x in range(0, context.CLIARGS['verbosity'])])
|
2015-05-01 22:29:15 +00:00
|
|
|
|
|
|
|
# Attempt to use the inventory passed in as an argument
|
2016-02-01 21:55:40 +00:00
|
|
|
# It might not yet have been downloaded so use localhost as default
|
2017-09-27 23:50:36 +00:00
|
|
|
inv_opts = self._get_inv_cli()
|
2017-10-25 23:55:48 +00:00
|
|
|
if not inv_opts:
|
|
|
|
inv_opts = " -i localhost, "
|
2019-03-12 20:15:14 +00:00
|
|
|
# avoid interpreter discovery since we already know which interpreter to use on localhost
|
|
|
|
inv_opts += '-e %s ' % shlex_quote('ansible_python_interpreter=%s' % sys.executable)
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2017-12-14 03:16:54 +00:00
|
|
|
# SCM specific options
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['module_name'] == 'git':
|
|
|
|
repo_opts = "name=%s dest=%s" % (context.CLIARGS['url'], context.CLIARGS['dest'])
|
|
|
|
if context.CLIARGS['checkout']:
|
|
|
|
repo_opts += ' version=%s' % context.CLIARGS['checkout']
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['accept_host_key']:
|
2015-05-01 22:29:15 +00:00
|
|
|
repo_opts += ' accept_hostkey=yes'
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['private_key_file']:
|
|
|
|
repo_opts += ' key_file=%s' % context.CLIARGS['private_key_file']
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['verify']:
|
2015-06-10 07:27:25 +00:00
|
|
|
repo_opts += ' verify_commit=yes'
|
2015-12-13 10:19:50 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['tracksubs']:
|
2016-06-23 13:02:34 +00:00
|
|
|
repo_opts += ' track_submodules=yes'
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if not context.CLIARGS['fullclone']:
|
2015-12-13 09:56:47 +00:00
|
|
|
repo_opts += ' depth=1'
|
2018-12-18 02:10:59 +00:00
|
|
|
elif context.CLIARGS['module_name'] == 'subversion':
|
|
|
|
repo_opts = "repo=%s dest=%s" % (context.CLIARGS['url'], context.CLIARGS['dest'])
|
|
|
|
if context.CLIARGS['checkout']:
|
|
|
|
repo_opts += ' revision=%s' % context.CLIARGS['checkout']
|
|
|
|
if not context.CLIARGS['fullclone']:
|
2017-12-14 03:16:54 +00:00
|
|
|
repo_opts += ' export=yes'
|
2018-12-18 02:10:59 +00:00
|
|
|
elif context.CLIARGS['module_name'] == 'hg':
|
|
|
|
repo_opts = "repo=%s dest=%s" % (context.CLIARGS['url'], context.CLIARGS['dest'])
|
|
|
|
if context.CLIARGS['checkout']:
|
|
|
|
repo_opts += ' revision=%s' % context.CLIARGS['checkout']
|
|
|
|
elif context.CLIARGS['module_name'] == 'bzr':
|
|
|
|
repo_opts = "name=%s dest=%s" % (context.CLIARGS['url'], context.CLIARGS['dest'])
|
|
|
|
if context.CLIARGS['checkout']:
|
|
|
|
repo_opts += ' version=%s' % context.CLIARGS['checkout']
|
2017-12-14 03:16:54 +00:00
|
|
|
else:
|
2018-12-18 02:10:59 +00:00
|
|
|
raise AnsibleOptionsError('Unsupported (%s) SCM module for pull, choices are: %s'
|
|
|
|
% (context.CLIARGS['module_name'],
|
|
|
|
','.join(self.REPO_CHOICES)))
|
2017-12-14 03:16:54 +00:00
|
|
|
|
|
|
|
# options common to all supported SCMS
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['clean']:
|
2017-12-14 03:16:54 +00:00
|
|
|
repo_opts += ' force=yes'
|
2015-12-10 10:16:21 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
path = module_loader.find_plugin(context.CLIARGS['module_name'])
|
2015-05-01 22:29:15 +00:00
|
|
|
if path is None:
|
2018-12-18 02:10:59 +00:00
|
|
|
raise AnsibleOptionsError(("module '%s' not found.\n" % context.CLIARGS['module_name']))
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2015-06-09 21:29:46 +00:00
|
|
|
bin_path = os.path.dirname(os.path.abspath(sys.argv[0]))
|
2016-01-29 23:46:56 +00:00
|
|
|
# hardcode local and inventory/host as this is just meant to fetch the repo
|
2018-12-18 02:10:59 +00:00
|
|
|
cmd = '%s/ansible %s %s -m %s -a "%s" all -l "%s"' % (bin_path, inv_opts, base_opts,
|
|
|
|
context.CLIARGS['module_name'],
|
|
|
|
repo_opts, limit_opts)
|
|
|
|
for ev in context.CLIARGS['extra_vars']:
|
2019-01-31 21:55:10 +00:00
|
|
|
cmd += ' -e %s' % shlex_quote(ev)
|
2015-05-01 22:29:15 +00:00
|
|
|
|
|
|
|
# Nap?
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['sleep']:
|
|
|
|
display.display("Sleeping for %d seconds..." % context.CLIARGS['sleep'])
|
|
|
|
time.sleep(context.CLIARGS['sleep'])
|
2015-05-01 22:29:15 +00:00
|
|
|
|
|
|
|
# RUN the Checkout command
|
2015-12-04 04:47:02 +00:00
|
|
|
display.debug("running ansible with VCS module to checkout repo")
|
|
|
|
display.vvvv('EXEC: %s' % cmd)
|
2018-03-19 16:10:50 +00:00
|
|
|
rc, b_out, b_err = run_cmd(cmd, live=True)
|
2015-05-01 22:29:15 +00:00
|
|
|
|
|
|
|
if rc != 0:
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['force']:
|
2015-11-10 19:40:55 +00:00
|
|
|
display.warning("Unable to update repository. Continuing with (forced) run of playbook.")
|
2015-05-01 22:29:15 +00:00
|
|
|
else:
|
|
|
|
return rc
|
2018-12-18 02:10:59 +00:00
|
|
|
elif context.CLIARGS['ifchanged'] and b'"changed": true' not in b_out:
|
2015-11-10 19:40:55 +00:00
|
|
|
display.display("Repository has not changed, quitting.")
|
2015-05-01 22:29:15 +00:00
|
|
|
return 0
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
playbook = self.select_playbook(context.CLIARGS['dest'])
|
2015-05-01 22:29:15 +00:00
|
|
|
if playbook is None:
|
|
|
|
raise AnsibleOptionsError("Could not find a playbook to run.")
|
|
|
|
|
|
|
|
# Build playbook command
|
|
|
|
cmd = '%s/ansible-playbook %s %s' % (bin_path, base_opts, playbook)
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['vault_password_files']:
|
|
|
|
for vault_password_file in context.CLIARGS['vault_password_files']:
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
cmd += " --vault-password-file=%s" % vault_password_file
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['vault_ids']:
|
|
|
|
for vault_id in context.CLIARGS['vault_ids']:
|
2017-12-06 13:06:35 +00:00
|
|
|
cmd += " --vault-id=%s" % vault_id
|
2017-09-27 23:50:36 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
for ev in context.CLIARGS['extra_vars']:
|
2019-01-31 21:55:10 +00:00
|
|
|
cmd += ' -e %s' % shlex_quote(ev)
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 17:27:44 +00:00
|
|
|
if context.CLIARGS['become_ask_pass']:
|
2015-12-04 04:47:02 +00:00
|
|
|
cmd += ' --ask-become-pass'
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['skip_tags']:
|
|
|
|
cmd += ' --skip-tags "%s"' % to_native(u','.join(context.CLIARGS['skip_tags']))
|
|
|
|
if context.CLIARGS['tags']:
|
|
|
|
cmd += ' -t "%s"' % to_native(u','.join(context.CLIARGS['tags']))
|
|
|
|
if context.CLIARGS['subset']:
|
|
|
|
cmd += ' -l "%s"' % context.CLIARGS['subset']
|
2016-02-01 21:55:40 +00:00
|
|
|
else:
|
|
|
|
cmd += ' -l "%s"' % limit_opts
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['check']:
|
2017-08-14 15:20:05 +00:00
|
|
|
cmd += ' -C'
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['diff']:
|
2018-08-28 19:15:14 +00:00
|
|
|
cmd += ' -D'
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
os.chdir(context.CLIARGS['dest'])
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2017-09-27 23:50:36 +00:00
|
|
|
# redo inventory options as new files might exist now
|
|
|
|
inv_opts = self._get_inv_cli()
|
|
|
|
if inv_opts:
|
|
|
|
cmd += inv_opts
|
|
|
|
|
2015-05-01 22:29:15 +00:00
|
|
|
# RUN THE PLAYBOOK COMMAND
|
2015-12-04 04:47:02 +00:00
|
|
|
display.debug("running ansible-playbook to do actual work")
|
|
|
|
display.debug('EXEC: %s' % cmd)
|
2018-03-19 16:10:50 +00:00
|
|
|
rc, b_out, b_err = run_cmd(cmd, live=True)
|
2015-05-01 22:29:15 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['purge']:
|
2015-05-01 22:29:15 +00:00
|
|
|
os.chdir('/')
|
|
|
|
try:
|
2018-12-18 02:10:59 +00:00
|
|
|
shutil.rmtree(context.CLIARGS['dest'])
|
2015-08-27 06:16:11 +00:00
|
|
|
except Exception as e:
|
2018-12-18 02:10:59 +00:00
|
|
|
display.error(u"Failed to remove %s: %s" % (context.CLIARGS['dest'], to_text(e)))
|
2015-05-01 22:29:15 +00:00
|
|
|
|
|
|
|
return rc
|
|
|
|
|
2018-12-20 04:45:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def try_playbook(path):
|
2015-05-01 22:29:15 +00:00
|
|
|
if not os.path.exists(path):
|
|
|
|
return 1
|
|
|
|
if not os.access(path, os.R_OK):
|
|
|
|
return 2
|
|
|
|
return 0
|
|
|
|
|
2018-12-20 04:45:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def select_playbook(path):
|
2015-05-01 22:29:15 +00:00
|
|
|
playbook = None
|
2018-12-18 02:10:59 +00:00
|
|
|
if context.CLIARGS['args'] and context.CLIARGS['args'][0] is not None:
|
|
|
|
playbook = os.path.join(path, context.CLIARGS['args'][0])
|
2018-12-20 04:45:47 +00:00
|
|
|
rc = PullCLI.try_playbook(playbook)
|
2015-05-01 22:29:15 +00:00
|
|
|
if rc != 0:
|
2018-12-20 04:45:47 +00:00
|
|
|
display.warning("%s: %s" % (playbook, PullCLI.PLAYBOOK_ERRORS[rc]))
|
2015-05-01 22:29:15 +00:00
|
|
|
return None
|
|
|
|
return playbook
|
|
|
|
else:
|
|
|
|
fqdn = socket.getfqdn()
|
|
|
|
hostpb = os.path.join(path, fqdn + '.yml')
|
|
|
|
shorthostpb = os.path.join(path, fqdn.split('.')[0] + '.yml')
|
2018-12-20 04:45:47 +00:00
|
|
|
localpb = os.path.join(path, PullCLI.DEFAULT_PLAYBOOK)
|
2015-05-01 22:29:15 +00:00
|
|
|
errors = []
|
|
|
|
for pb in [hostpb, shorthostpb, localpb]:
|
2018-12-20 04:45:47 +00:00
|
|
|
rc = PullCLI.try_playbook(pb)
|
2015-05-01 22:29:15 +00:00
|
|
|
if rc == 0:
|
|
|
|
playbook = pb
|
|
|
|
break
|
|
|
|
else:
|
2018-12-20 04:45:47 +00:00
|
|
|
errors.append("%s: %s" % (pb, PullCLI.PLAYBOOK_ERRORS[rc]))
|
2015-05-01 22:29:15 +00:00
|
|
|
if playbook is None:
|
2015-11-10 19:40:55 +00:00
|
|
|
display.warning("\n".join(errors))
|
2015-05-01 22:29:15 +00:00
|
|
|
return playbook
|