Skip to content

Commit

Permalink
Added ansible limit host parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Abarca committed Jun 27, 2024
1 parent 2e25020 commit 70945c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public class AnsibleInventoryList {

private AnsibleVault ansibleVault;
private VaultPrompt vaultPrompt;
private List<String> limits;

private File tempInternalVaultFile;
private File tempVaultFile;
private File vaultPromptFile;
private File tempLimitFile;

public static final String ANSIBLE_INVENTORY = "ansible-inventory";

Expand All @@ -55,6 +58,7 @@ public String getNodeList() throws Exception {
List<VaultPrompt> stdinVariables = new ArrayList<>();

processAnsibleVault(stdinVariables, procArgs);
processLimit(procArgs);

if(debug){
System.out.println("getNodeList " + procArgs);
Expand Down Expand Up @@ -115,4 +119,21 @@ private void processAnsibleVault(List<VaultPrompt> stdinVariables, List<String>
procArgs.add(tempVaultFile.getAbsolutePath());
}
}

private void processLimit(List<String> procArgs) throws IOException {
if (limits != null && limits.size() == 1) {
procArgs.add("-l");
procArgs.add(limits.get(0));

} else if (limits != null && limits.size() > 1) {
StringBuilder sb = new StringBuilder();
for (String limit : limits) {
sb.append(limit).append("\n");
}
tempLimitFile = AnsibleUtil.createTemporaryFile("targets", sb.toString());

procArgs.add("-l");
procArgs.add("@" + tempLimitFile.getAbsolutePath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ public String getNodesFromInventory(AnsibleRunner.AnsibleRunnerBuilder runnerBui
ansibleInventoryListBuilder.vaultPrompt(vaultPrompt);
}

if (!runner.getLimits().isEmpty()) {
ansibleInventoryListBuilder.limits(runner.getLimits());
}

AnsibleInventoryList inventoryList = this.ansibleInventoryListBuilder.build();

try {
Expand Down

0 comments on commit 70945c6

Please sign in to comment.