private void run(Completion completion) {
new PrepareAnsible().setTargetIp(msg.getTargetIp()).prepare();
logger.debug(String.format("start running ansible for playbook[%s]", msg.getPlayBookPath()));
Map<String, Object> arguments = new HashMap<String, Object>();
if (msg.getArguments() != null) {
arguments.putAll(msg.getArguments());
arguments.put("host", msg.getTargetIp());
arguments.put("zstack_root", AnsibleGlobalProperty.ZSTACK_ROOT);
arguments.put("pkg_zstacklib", AnsibleGlobalProperty.ZSTACKLIB_PACKAGE_NAME);
arguments.putAll(getVariables());
String playBookPath = msg.getPlayBookPath();
if (!playBookPath.contains("py")) {
arguments.put("ansible_ssh_user", arguments.get("remote_user"));
String executable = msg.getAnsibleExecutable() == null ? AnsibleGlobalProperty.EXECUTABLE : msg.getAnsibleExecutable();
long timeout = TimeUnit.MILLISECONDS.toSeconds(msg.getTimeout());
try {
String output;
if (AnsibleGlobalProperty.DEBUG_MODE2) {
output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '%s' | tee -a %s",
AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments), AnsibleConstant.LOG_PATH),
AnsibleConstant.ROOT_DIR);
} else if (AnsibleGlobalProperty.DEBUG_MODE) {
output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '%s'",
AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments)),
AnsibleConstant.ROOT_DIR);
} else {
output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s --private-key %s -e '%s'",