def gen_facts_cache(self, remote_dir, inventory_file, threads): # Set local_tmp dir different for any VM os.environ['DEFAULT_LOCAL_TMP'] = remote_dir + "/.ansible_tmp" # it must be set before doing the import from IM.ansible_utils.ansible_launcher import AnsibleThread playbook_file = "/tmp/gen_facts_cache.yml" with open(playbook_file, "w+") as f: f.write(" - hosts: allnowindows\n") result = Queue() t = AnsibleThread(result, self.logger, playbook_file, threads, CtxtAgent.PK_FILE, None, CtxtAgent.PLAYBOOK_RETRIES, inventory_file) t.start() return (t, result)
def test_ansible_thread(self): result = Queue() tests_path = os.path.dirname(os.path.abspath(__file__)) play_file_path = os.path.join(tests_path, "../files/play.yaml") inventory = os.path.join(tests_path, "../files/inventory") ansible_process = AnsibleThread(result, StringIO(), play_file_path, None, 1, None, "password", 1, inventory, "username") ansible_process.run() _, (return_code, _), output = result.get() self.assertEqual(return_code, 0) self.assertIn("failed=0", output.getvalue()) self.assertIn("changed=2", output.getvalue()) print(output.getvalue())
def LaunchAnsiblePlaybook(output, remote_dir, playbook_file, vm, threads, inventory_file, pk_file, retries, change_pass_ok, vault_pass): CtxtAgent.logger.debug('Call Ansible') extra_vars = {'IM_HOST': vm['ip'] + "_" + str(vm['remote_port'])} user = None if vm['os'] == "windows": gen_pk_file = None passwd = vm['passwd'] if 'new_passwd' in vm and vm['new_passwd'] and change_pass_ok: passwd = vm['new_passwd'] else: passwd = vm['passwd'] if 'new_passwd' in vm and vm['new_passwd'] and change_pass_ok: passwd = vm['new_passwd'] if pk_file: gen_pk_file = pk_file else: if vm['private_key'] and not vm['passwd']: gen_pk_file = "/tmp/pk_" + vm['ip'] + ".pem" # If the file exists do not create it again if not os.path.isfile(gen_pk_file): pk_out = open(gen_pk_file, 'w') pk_out.write(vm['private_key']) pk_out.close() os.chmod(gen_pk_file, 0400) else: gen_pk_file = None # Set local_tmp dir different for any VM os.environ['DEFAULT_LOCAL_TMP'] = remote_dir + "/.ansible_tmp" # it must be set before doing the import from IM.ansible_utils.ansible_launcher import AnsibleThread result = Queue() t = AnsibleThread(result, output, playbook_file, None, threads, gen_pk_file, passwd, retries, inventory_file, user, vault_pass, extra_vars) t.start() return (t, result)