def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host self.ipv6 = ':' in self.host self.port = port self.user = str(user) self.password = password self.private_key_file = private_key_file self.HASHED_KEY_MAGIC = "|1|" self.has_pipelining = True #self.instance_id = "instance-00000045" # C.get_config(C.p, "xos", "instance_id", "INSTANCE_ID", None) #self.sliver_name = "onlab_hpc-355" # C.get_config(C.p, "xos", "sliver_name", "SLIVER_NAME", None) inject = {} inject = utils.combine_vars( inject, self.runner.inventory.get_variables(self.host)) self.instance_id = inject["instance_id"] self.sliver_name = inject["sliver_name"] fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_EX) self.cp_dir = utils.prepare_writeable_dir('$HOME/.ansible/cp', mode=0700) fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_UN)
def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host self.ipv6 = ':' in self.host self.port = port self.user = user self.password = password self.private_key_file = private_key_file self.cp_dir = utils.prepare_writeable_dir('$HOME/.ansible/cp',mode=0700) self.HASHED_KEY_MAGIC = "|1|"
def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host self.ipv6 = ":" in self.host self.port = port self.user = str(user) self.password = password self.private_key_file = private_key_file self.HASHED_KEY_MAGIC = "|1|" self.has_pipelining = True fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_EX) self.cp_dir = utils.prepare_writeable_dir("$HOME/.ansible/cp", mode=0700) fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_UN)
def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host self.ipv6 = ':' in self.host self.port = port self.user = user self.password = password self.private_key_file = private_key_file self.HASHED_KEY_MAGIC = "|1|" self.has_pipelining = True fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_EX) self.cp_dir = utils.prepare_writeable_dir('$HOME/.ansible/cp',mode=0700) fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_UN)
def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host self.ipv6 = ':' in self.host self.port = port self.user = str(user) self.password = password self.private_key_file = private_key_file self.HASHED_KEY_MAGIC = "|1|" self.has_pipelining = True # TODO: add pbrun, pfexec self.become_methods_supported=['sudo', 'su', 'pbrun'] self.cp_dir = utils.prepare_writeable_dir('$HOME/.ansible/cp',mode=0700)
def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host self.ipv6 = ':' in self.host self.port = port self.user = str(user) self.password = password self.private_key_file = private_key_file self.HASHED_KEY_MAGIC = "|1|" self.has_pipelining = True # TODO: add pbrun, pfexec self.become_methods_supported=['sudo', 'su', 'pbrun'] fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_EX) self.cp_dir = utils.prepare_writeable_dir('$HOME/.ansible/cp',mode=0700) fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_UN)
def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host self.ipv6 = ":" in self.host self.port = port self.user = str(user) self.password = password self.private_key_file = private_key_file self.HASHED_KEY_MAGIC = "|1|" self.has_pipelining = True # self.instance_id = "instance-00000045" # C.get_config(C.p, "xos", "instance_id", "INSTANCE_ID", None) # self.sliver_name = "onlab_hpc-355" # C.get_config(C.p, "xos", "sliver_name", "SLIVER_NAME", None) inject = {} inject = utils.combine_vars(inject, self.runner.inventory.get_variables(self.host)) self.instance_id = inject["instance_id"] self.sliver_name = inject["sliver_name"] fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_EX) self.cp_dir = utils.prepare_writeable_dir("$HOME/.ansible/cp", mode=0700) fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_UN)
def generate_retry_inventory(self, replay_hosts): ''' called by /usr/bin/ansible when a playbook run fails. It generates a inventory that allows re-running on ONLY the failed hosts. This may duplicate some variable information in group_vars/host_vars but that is ok, and expected. ''' buf = StringIO.StringIO() for x in replay_hosts: buf.write("%s\n" % x) basedir = self.inventory.basedir() filename = "%s.retry" % os.path.basename(self.filename) filename = filename.replace(".yml", "") retry_dir = utils.prepare_writeable_dir('$HOME/.ansible', mode=0700) filename = os.path.join(retry_dir, filename) try: fd = open(filename, 'w') fd.write(buf.getvalue()) fd.close() return filename except: pass return None
def run(self, options, args): ''' use Runner lib to do SSH things ''' pattern = args[0] sshpass = becomepass = vault_pass = become_method = None # Never ask for an SSH password when we run with local connection if options.connection == "local": options.ask_pass = False else: options.ask_pass = options.ask_pass or C.DEFAULT_ASK_PASS options.ask_vault_pass = options.ask_vault_pass or C.DEFAULT_ASK_VAULT_PASS # become utils.normalize_become_options(options) prompt_method = utils.choose_pass_prompt(options) (sshpass, becomepass, vault_pass) = utils.ask_passwords(ask_pass=options.ask_pass, become_ask_pass=options.become_ask_pass, ask_vault_pass=options.ask_vault_pass, become_method=prompt_method) # read vault_pass from a file if not options.ask_vault_pass and options.vault_password_file: vault_pass = utils.read_vault_file(options.vault_password_file) extra_vars = utils.parse_extra_vars(options.extra_vars, vault_pass) inventory_manager = inventory.Inventory(options.inventory, vault_password=vault_pass) if options.subset: inventory_manager.subset(options.subset) hosts = inventory_manager.list_hosts(pattern) if len(hosts) == 0: callbacks.display("No hosts matched", stderr=True) sys.exit(0) if options.listhosts: for host in hosts: callbacks.display(' %s' % host) sys.exit(0) if options.module_name in ['command','shell'] and not options.module_args: callbacks.display("No argument passed to %s module" % options.module_name, color='red', stderr=True) sys.exit(1) if options.tree: utils.prepare_writeable_dir(options.tree) runner = Runner( module_name=options.module_name, module_path=options.module_path, module_args=options.module_args, remote_user=options.remote_user, remote_pass=sshpass, inventory=inventory_manager, timeout=options.timeout, private_key_file=options.private_key_file, forks=options.forks, pattern=pattern, callbacks=self.callbacks, transport=options.connection, subset=options.subset, check=options.check, diff=options.check, vault_pass=vault_pass, become=options.become, become_method=options.become_method, become_pass=becomepass, become_user=options.become_user, extra_vars=extra_vars, ) import pdb pdb.set_trace() if options.seconds: callbacks.display("background launch...\n\n", color='cyan') results, poller = runner.run_async(options.seconds) results = self.poll_while_needed(poller, options) else: results = runner.run() return (runner, results)
def __call__(self, argv, help): import ansible.constants as C from ansible.runner import Runner from ansible import callbacks from mr.awsome.ansible.inventory import Inventory from ansible import utils parser = utils.base_parser( constants=C, runas_opts=True, subset_opts=True, output_opts=True, check_opts=True, diff_opts=False, usage='aws ansible <host-pattern> [options]' ) parser.remove_option('-i') parser.remove_option('-k') parser.add_option('-a', '--args', dest='module_args', help="module arguments", default=C.DEFAULT_MODULE_ARGS) parser.add_option('-m', '--module-name', dest='module_name', help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME, default=C.DEFAULT_MODULE_NAME) options, args = parser.parse_args(argv) if len(args) == 0 or len(args) > 1: parser.print_help() sys.exit(1) cbs = callbacks.CliRunnerCallbacks() cbs.options = options pattern = args[0] patch_connect(self.aws) inventory_manager = Inventory(self.aws) if options.subset: inventory_manager.subset(options.subset) hosts = inventory_manager.list_hosts(pattern) if len(hosts) == 0: callbacks.display("No hosts matched", stderr=True) sys.exit(1) if options.listhosts: for host in hosts: callbacks.display(' %s' % host) sys.exit(0) if ((options.module_name == 'command' or options.module_name == 'shell') and not options.module_args): callbacks.display("No argument passed to %s module" % options.module_name, color='red', stderr=True) sys.exit(1) sudopass = None options.ask_sudo_pass = options.ask_sudo_pass or C.DEFAULT_ASK_SUDO_PASS sudopass = utils.ask_passwords(ask_sudo_pass=options.ask_sudo_pass) if options.sudo_user or options.ask_sudo_pass: options.sudo = True options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER if options.tree: utils.prepare_writeable_dir(options.tree) runner = Runner( module_name=options.module_name, module_path=options.module_path, module_args=options.module_args, remote_user=options.remote_user, inventory=inventory_manager, timeout=options.timeout, private_key_file=options.private_key_file, forks=options.forks, pattern=pattern, callbacks=cbs, sudo=options.sudo, sudo_pass=sudopass,sudo_user=options.sudo_user, transport='ssh', subset=options.subset, check=options.check, diff=options.check) results = runner.run() for result in results['contacted'].values(): if 'failed' in result or result.get('rc', 0) != 0: sys.exit(2) if results['dark']: sys.exit(3)