Skip to content

Commit

Permalink
Ansible-runner: Fix getting status of playbook
Browse files Browse the repository at this point in the history
Ansible-runner command does nolonger create status file when running but
only after it finished. This patch changes the logic to return status
running acording to daemon.log which is created right after the playbook
is started.
  • Loading branch information
mnecas committed Jun 15, 2022
1 parent 07dc9fc commit 428f6be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ public void doPolling(Guid cmdId, List<Guid> childCmdIds) {

AnsibleReturnValue ret = new AnsibleReturnValue(AnsibleReturnCode.ERROR);
ret.setLogFile(runnerClient.getLogger().getLogFile());
String msg = "";
int totalEvents;
// Get the current status of the playbook:
AnsibleRunnerClient.PlaybookStatus playbookStatus = runnerClient.getPlaybookStatus(playUuid);
String status = playbookStatus.getStatus();
msg = playbookStatus.getMsg();
String msg = playbookStatus.getMsg();
// Process the events if the playbook is running:
totalEvents = runnerClient.getTotalEvents(playUuid);

Expand All @@ -82,7 +80,7 @@ public void doPolling(Guid cmdId, List<Guid> childCmdIds) {
ret.setAnsibleReturnCode(AnsibleReturnCode.OK);
command.setSucceeded(true);
command.setCommandStatus(CommandStatus.SUCCEEDED);
} else if (status.equalsIgnoreCase("unknown")) {
} else if (msg.equalsIgnoreCase("unknown")) {
// ignore and continue:
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,19 @@ protected String formatCommandVariables(Map<String, Object> variables, String pl
public PlaybookStatus getPlaybookStatus(String playUuid) {
String status = "";
String rc = "";
String privateRunDir = String.format("%1$s/%2$s/", AnsibleConstants.ANSIBLE_RUNNER_PATH, playUuid);
String playData = String.format("%1$s/%2$s/artifacts/%2$s/", AnsibleConstants.ANSIBLE_RUNNER_PATH, playUuid);
try {
if (!Files.exists(Paths.get(String.format("%1$s/status", playData)))) {
if (Files.exists(Paths.get(String.format("%1$s/status", playData)))) {
status = Files.readString(Paths.get(String.format("%1$s/status", playData)));
rc = Files.readString(Paths.get(String.format("%1$s/rc", playData)));
} else if (Files.exists(Paths.get(String.format("%1$s/daemon.log", privateRunDir)))) {
// artifacts are not yet present, try to fetch them in the next polling round
return new PlaybookStatus("unknown", "");
return new PlaybookStatus("", "running");
} else {
log.warn(String.format("The playbook failed with unknow error at: %1$s", playData));
return new PlaybookStatus("", "failed");
}
status = Files.readString(Paths.get(String.format("%1$s/status", playData)));
rc = Files.readString(Paths.get(String.format("%1$s/rc", playData)));
} catch (Exception e) {
throw new AnsibleRunnerCallException(
String.format("Failed to read playbook result at: %1$s", playData), e);
Expand Down

0 comments on commit 428f6be

Please sign in to comment.