Skip to content

Commit 3a85dfa

Browse files
Merge pull request #405 from rundeck-plugins/RPL-69-unable-to-modify-ansible-tmp-dir-path
RPL-69 Enable custom tmp on ansible
2 parents 2fb4c34 + 289211d commit 3a85dfa

19 files changed

+150
-29
lines changed

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleInlineInventoryBuilder.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.dtolabs.rundeck.core.common.INodeEntry;
44
import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException;
5+
import com.rundeck.plugins.ansible.util.AnsibleUtil;
56

67
import java.io.File;
78
import java.io.PrintWriter;
@@ -11,14 +12,16 @@
1112
public class AnsibleInlineInventoryBuilder {
1213

1314
private final String inline_inventory;
15+
private final String customTmpDirPath;
1416

15-
public AnsibleInlineInventoryBuilder(String inline_inventory) {
17+
public AnsibleInlineInventoryBuilder(String inline_inventory,String customTmpDirPath) {
1618
this.inline_inventory = inline_inventory;
19+
this.customTmpDirPath = customTmpDirPath;
1720
}
1821

1922
public File buildInventory() throws ConfigurationException {
2023
try {
21-
File file = File.createTempFile("ansible-inventory", ".inventory");
24+
File file = AnsibleUtil.createTemporaryFile("ansible-inventory", ".inventory","",customTmpDirPath);
2225
file.deleteOnExit();
2326
PrintWriter writer = new PrintWriter(file);
2427
writer.write(inline_inventory);

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleInventoryBuilder.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
import java.util.HashMap;
1111

1212
import com.google.gson.Gson;
13+
import com.rundeck.plugins.ansible.util.AnsibleUtil;
1314

1415
public class AnsibleInventoryBuilder {
1516

1617
private final Collection<INodeEntry> nodes;
18+
private final String customTmpDirPath;
1719

18-
public AnsibleInventoryBuilder(Collection<INodeEntry> nodes) {
20+
public AnsibleInventoryBuilder(Collection<INodeEntry> nodes, String customTmpDirPath ) {
1921
this.nodes = nodes;
22+
this.customTmpDirPath = customTmpDirPath;
2023
}
2124

2225
public File buildInventory() throws ConfigurationException {
2326
try {
24-
File file = File.createTempFile("ansible-inventory", ".json");
27+
File file = AnsibleUtil.createTemporaryFile("ansible-inventory", ".json","",customTmpDirPath);
2528
file.deleteOnExit();
2629
PrintWriter writer = new PrintWriter(file);
2730
AnsibleInventory ai = new AnsibleInventory();

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleInventoryList.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class AnsibleInventoryList {
3535
private File tempVaultFile;
3636
private File vaultPromptFile;
3737
private File tempLimitFile;
38+
private String customTmpDirPath;
3839

3940
public static final String ANSIBLE_INVENTORY_COMMAND = "ansible-inventory";
4041

@@ -139,7 +140,7 @@ private void processAnsibleVault(List<VaultPrompt> stdinVariables, List<String>
139140
if (vaultPrompt == null) { return; }
140141

141142
if(ansibleVault == null){
142-
tempInternalVaultFile = AnsibleVault.createVaultScriptAuth("ansible-script-vault");
143+
tempInternalVaultFile = AnsibleVault.createVaultScriptAuth("ansible-script-vault",customTmpDirPath);
143144
ansibleVault = AnsibleVault.builder()
144145
.masterPassword(vaultPrompt.getVaultPassword())
145146
.vaultPasswordScriptFile(tempInternalVaultFile)
@@ -165,7 +166,7 @@ private void processLimit(List<String> procArgs) throws IOException {
165166
for (String limit : limits) {
166167
sb.append(limit).append("\n");
167168
}
168-
tempLimitFile = AnsibleUtil.createTemporaryFile("targets", sb.toString());
169+
tempLimitFile = AnsibleUtil.createTemporaryFile("","targets", sb.toString(),customTmpDirPath);
169170

170171
procArgs.add("-l");
171172
procArgs.add("@" + tempLimitFile.getAbsolutePath());

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunner.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ public static AnsibleRunner buildAnsibleRunner(AnsibleRunnerContextBuilder conte
292292
File tempBecameVarsFile ;
293293
File vaultPromptFile;
294294

295+
String customTmpDirPath;
296+
295297
public void deleteTempDirectory(Path tempDirectory) throws IOException {
296298
Files.walkFileTree(tempDirectory, new SimpleFileVisitor<Path>() {
297299
@Override
@@ -333,11 +335,11 @@ public int run() throws Exception {
333335
if (baseDirectory == null) {
334336
// Use a temporary directory and mark it for possible removal later
335337
this.usingTempDirectory = true;
336-
baseDirectory = Files.createTempDirectory("ansible-rundeck");
338+
baseDirectory = Files.createTempDirectory(Path.of(customTmpDirPath),"ansible-rundeck");
337339
}
338340

339341
if(ansibleVault==null){
340-
tempInternalVaultFile = AnsibleVault.createVaultScriptAuth("ansible-script-vault");
342+
tempInternalVaultFile = AnsibleVault.createVaultScriptAuth("ansible-script-vault",customTmpDirPath);
341343
ansibleVault = AnsibleVault.builder()
342344
.baseDirectory(baseDirectory)
343345
.masterPassword(AnsibleUtil.randomString())
@@ -373,7 +375,7 @@ public int run() throws Exception {
373375
} else if (type == AnsibleCommand.PlaybookPath) {
374376
procArgs.add(playbook);
375377
} else if (type == AnsibleCommand.PlaybookInline) {
376-
tempPlaybook = AnsibleUtil.createTemporaryFile("playbook", playbook);
378+
tempPlaybook = AnsibleUtil.createTemporaryFile("","playbook", playbook,customTmpDirPath);
377379
procArgs.add(tempPlaybook.getAbsolutePath());
378380
}
379381

@@ -405,7 +407,7 @@ public int run() throws Exception {
405407
for (String limit : limits) {
406408
sb.append(limit).append("\n");
407409
}
408-
tempFile = AnsibleUtil.createTemporaryFile("targets", sb.toString());
410+
tempFile = AnsibleUtil.createTemporaryFile("","targets", sb.toString(),customTmpDirPath);
409411

410412
procArgs.add("-l");
411413
procArgs.add("@" + tempFile.getAbsolutePath());
@@ -422,13 +424,13 @@ public int run() throws Exception {
422424
addeExtraVars = encryptExtraVarsKey(extraVars);
423425
}
424426

425-
tempVarsFile = AnsibleUtil.createTemporaryFile("extra-vars", addeExtraVars);
427+
tempVarsFile = AnsibleUtil.createTemporaryFile("","extra-vars", addeExtraVars,customTmpDirPath);
426428
procArgs.add("--extra-vars" + "=" + "@" + tempVarsFile.getAbsolutePath());
427429
}
428430

429431
if (sshPrivateKey != null && !sshPrivateKey.isEmpty()) {
430432
String privateKeyData = sshPrivateKey.replaceAll("\r\n", "\n");
431-
tempPkFile = AnsibleUtil.createTemporaryFile("id_rsa", privateKeyData);
433+
tempPkFile = AnsibleUtil.createTemporaryFile("","id_rsa", privateKeyData,customTmpDirPath);
432434

433435
// Only the owner can read and write
434436
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
@@ -454,7 +456,7 @@ public int run() throws Exception {
454456
finalextraVarsPassword = encryptExtraVarsKey(extraVarsPassword);
455457
}
456458

457-
tempSshVarsFile = AnsibleUtil.createTemporaryFile("ssh-extra-vars", finalextraVarsPassword);
459+
tempSshVarsFile = AnsibleUtil.createTemporaryFile("","ssh-extra-vars", finalextraVarsPassword,customTmpDirPath);
458460
procArgs.add("--extra-vars" + "=" + "@" + tempSshVarsFile.getAbsolutePath());
459461
}
460462

@@ -473,7 +475,7 @@ public int run() throws Exception {
473475
finalextraVarsPassword = encryptExtraVarsKey(extraVarsPassword);
474476
}
475477

476-
tempBecameVarsFile = AnsibleUtil.createTemporaryFile("become-extra-vars", finalextraVarsPassword);
478+
tempBecameVarsFile = AnsibleUtil.createTemporaryFile("","become-extra-vars", finalextraVarsPassword,customTmpDirPath);
477479
procArgs.add("--extra-vars" + "=" + "@" + tempBecameVarsFile.getAbsolutePath());
478480
}
479481
}
@@ -531,7 +533,7 @@ public int run() throws Exception {
531533
List<VaultPrompt> stdinVariables = new ArrayList<>();
532534

533535
if(useAnsibleVault || vaultPass != null ){
534-
vaultPromptFile = File.createTempFile("vault-prompt", ".log");
536+
vaultPromptFile = AnsibleUtil.createTemporaryFile("vault-prompt",".log","",customTmpDirPath);
535537
}
536538

537539
if (useAnsibleVault) {
@@ -677,7 +679,7 @@ public boolean registerKeySshAgent(String keyPath) throws Exception {
677679

678680
File tempPassVarsFile = null;
679681
if (sshPassphrase != null && sshPassphrase.length() > 0) {
680-
tempPassVarsFile = File.createTempFile("ansible-runner", "ssh-add-check");
682+
tempPassVarsFile = AnsibleUtil.createTemporaryFile("","ssh-add-check","",customTmpDirPath);
681683
tempPassVarsFile.setExecutable(true);
682684

683685
List<String> passScript = new ArrayList<>();

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerContextBuilder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.Paths;
2323

2424
import com.rundeck.plugins.ansible.plugin.AnsiblePluginGroup;
25+
import com.rundeck.plugins.ansible.util.AnsibleUtil;
2526
import lombok.Getter;
2627
import org.rundeck.storage.api.Path;
2728

@@ -605,7 +606,7 @@ public String getInventory() throws ConfigurationException {
605606

606607

607608
if (isGenerated != null && isGenerated) {
608-
File tempInventory = new AnsibleInventoryBuilder(this.nodes).buildInventory();
609+
File tempInventory = new AnsibleInventoryBuilder(this.nodes, AnsibleUtil.getCustomTmpPathDir(framework)).buildInventory();
609610
tempFiles.add(tempInventory);
610611
inventory = tempInventory.getAbsolutePath();
611612
return inventory;
@@ -625,7 +626,7 @@ public String getInventory() throws ConfigurationException {
625626
the builder gets the nodes from rundeck in rundeck node format and converts to ansible inventory
626627
we don't want that, we simply want the list we provided in ansible format
627628
*/
628-
File tempInventory = new AnsibleInlineInventoryBuilder(inline_inventory).buildInventory();
629+
File tempInventory = new AnsibleInlineInventoryBuilder(inline_inventory,AnsibleUtil.getCustomTmpPathDir(framework)).buildInventory();
629630
tempFiles.add(tempInventory);
630631
inventory = tempInventory.getAbsolutePath();
631632
return inventory;

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleVault.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public String encryptVariable(String key,
128128
}
129129

130130

131-
public static File createVaultScriptAuth(String suffix) throws IOException {
132-
File tempInternalVaultFile = File.createTempFile("ansible-runner", suffix + "-client.py");
131+
public static File createVaultScriptAuth(String suffix, String path) throws IOException {
132+
File tempInternalVaultFile = AnsibleUtil.createTemporaryFile("ansible-runner", suffix + "-client.py","",path);
133133

134134
try {
135135
Files.copy(AnsibleUtil.class.getClassLoader().getResourceAsStream("vault-client.py"),

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleFileCopier.java

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private String doFileCopy(
149149

150150
try {
151151
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
152+
runner.setCustomTmpDirPath(AnsibleUtil.getCustomTmpPathDir(contextBuilder.getFramework()));
152153
} catch (ConfigurationException e) {
153154
throw new FileCopierException("Error configuring Ansible.",AnsibleFailureReason.ParseArgumentsError, e);
154155
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleModuleWorkflowStep.java

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
8282

8383
try {
8484
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
85+
runner.setCustomTmpDirPath(AnsibleUtil.getCustomTmpPathDir(contextBuilder.getFramework()));
8586
} catch (ConfigurationException e) {
8687
throw new StepException("Error configuring Ansible runner: " + e.getMessage(), e, AnsibleException.AnsibleFailureReason.ParseArgumentsError);
8788
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleNodeExecutor.java

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public NodeExecutorResult executeCommand(ExecutionContext context, String[] comm
170170

171171
try {
172172
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
173+
runner.setCustomTmpDirPath(AnsibleUtil.getCustomTmpPathDir(contextBuilder.getFramework()));
173174
} catch (ConfigurationException e) {
174175
return NodeExecutorResultImpl.createFailure(AnsibleException.AnsibleFailureReason.ParseArgumentsError, e.getMessage(), node);
175176
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookInlineWorkflowNodeStep.java

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void executeNodeStep(
9393

9494
try {
9595
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
96+
runner.setCustomTmpDirPath(AnsibleUtil.getCustomTmpPathDir(contextBuilder.getFramework()));
9697
} catch (ConfigurationException e) {
9798
throw new NodeStepException("Error configuring Ansible runner: "+e.getMessage(), AnsibleException.AnsibleFailureReason.ParseArgumentsError,e.getMessage());
9899
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookInlineWorkflowStep.java

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
9595

9696
try {
9797
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
98+
runner.setCustomTmpDirPath(AnsibleUtil.getCustomTmpPathDir(contextBuilder.getFramework()));
9899
} catch (ConfigurationException e) {
99100
throw new StepException("Error configuring Ansible runner: " + e.getMessage(), e, AnsibleException.AnsibleFailureReason.ParseArgumentsError);
100101
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookWorflowNodeStep.java

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public void executeNodeStep(
9191

9292
try {
9393
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
94+
runner.setCustomTmpDirPath(AnsibleUtil.getCustomTmpPathDir(contextBuilder.getFramework()));
9495
} catch (ConfigurationException e) {
9596
throw new NodeStepException("Error configuring Ansible runner: "+e.getMessage(), AnsibleException.AnsibleFailureReason.ParseArgumentsError,e.getMessage());
9697
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookWorkflowStep.java

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
9494

9595
try {
9696
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
97+
runner.setCustomTmpDirPath(AnsibleUtil.getCustomTmpPathDir(contextBuilder.getFramework()));
9798
} catch (ConfigurationException e) {
9899
throw new StepException("Error configuring Ansible runner: " + e.getMessage(), e, AnsibleException.AnsibleFailureReason.ParseArgumentsError);
99100
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSource.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.rundeck.plugins.ansible.ansible.AnsibleInventoryList;
2626
import com.rundeck.plugins.ansible.ansible.AnsibleRunner;
2727
import com.rundeck.plugins.ansible.ansible.InventoryList;
28+
import com.rundeck.plugins.ansible.util.AnsibleUtil;
2829
import com.rundeck.plugins.ansible.util.VaultPrompt;
2930
import lombok.Setter;
3031
import lombok.extern.slf4j.Slf4j;
@@ -132,6 +133,8 @@ public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRun
132133

133134
protected boolean encryptExtraVars = false;
134135

136+
protected String customTmpDirPath;
137+
135138
@Setter
136139
private AnsibleInventoryList.AnsibleInventoryListBuilder ansibleInventoryListBuilder = null;
137140

@@ -189,7 +192,7 @@ public void configure(Properties configuration) throws ConfigurationException {
189192
configDataContext.put("context", configdata);
190193
executionDataContext = ScriptDataContextUtil.createScriptDataContextForProject(framework, project);
191194
executionDataContext.putAll(configDataContext);
192-
195+
customTmpDirPath = AnsibleUtil.getCustomTmpPathDir(framework);
193196
inventory = resolveProperty(AnsibleDescribable.ANSIBLE_INVENTORY,null,configuration,executionDataContext);
194197
gatherFacts = "true".equals(resolveProperty(AnsibleDescribable.ANSIBLE_GATHER_FACTS,null,configuration,executionDataContext));
195198
ignoreErrors = "true".equals(resolveProperty(AnsibleDescribable.ANSIBLE_IGNORE_ERRORS,null,configuration,executionDataContext));
@@ -260,6 +263,7 @@ public AnsibleRunner.AnsibleRunnerBuilder buildAnsibleRunner() throws ResourceMo
260263
if ("true".equals(System.getProperty("ansible.debug"))) {
261264
runnerBuilder.debug(true);
262265
}
266+
runnerBuilder.customTmpDirPath(AnsibleUtil.getCustomTmpPathDir(framework));
263267

264268
if (limit != null && limit.length() > 0) {
265269
List<String> limitList = new ArrayList<>();
@@ -420,7 +424,7 @@ public void processWithGatherFacts(NodeSetImpl nodes, AnsibleRunner.AnsibleRunne
420424
final Gson gson = new Gson();
421425
Path tempDirectory;
422426
try {
423-
tempDirectory = Files.createTempDirectory("ansible-hosts");
427+
tempDirectory = Files.createTempDirectory(Path.of(customTmpDirPath),"ansible-hosts");
424428
} catch (IOException e) {
425429
throw new ResourceModelSourceException("Error creating temporary directory: " + e.getMessage(), e);
426430
}
@@ -431,7 +435,7 @@ public void processWithGatherFacts(NodeSetImpl nodes, AnsibleRunner.AnsibleRunne
431435
} catch (IOException e) {
432436
throw new ResourceModelSourceException("Error copying files: " + e.getMessage(), e);
433437
}
434-
438+
runnerBuilder.customTmpDirPath(customTmpDirPath);
435439
runnerBuilder.tempDirectory(tempDirectory);
436440
runnerBuilder.retainTempDirectory(true);
437441

@@ -881,7 +885,7 @@ public String getNodesFromInventory(AnsibleRunner.AnsibleRunnerBuilder runnerBui
881885
}
882886

883887
AnsibleInventoryList inventoryList = this.ansibleInventoryListBuilder.build();
884-
888+
inventoryList.setCustomTmpDirPath(customTmpDirPath);
885889
try {
886890
return inventoryList.getNodeList();
887891
} catch (IOException | AnsibleException e) {

src/main/groovy/com/rundeck/plugins/ansible/util/AnsibleUtil.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.rundeck.plugins.ansible.util;
22

3+
import com.dtolabs.rundeck.core.common.Framework;
34
import com.dtolabs.rundeck.core.execution.ExecutionContext;
45
import com.dtolabs.rundeck.core.execution.proxy.DefaultSecretBundle;
56
import com.dtolabs.rundeck.core.execution.proxy.SecretBundle;
7+
import com.dtolabs.rundeck.core.execution.workflow.steps.PluginStepContextImpl;
68
import com.dtolabs.rundeck.core.plugins.configuration.Property;
9+
import com.dtolabs.rundeck.core.utils.PropertyLookup;
10+
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
711
import com.rundeck.plugins.ansible.ansible.AnsibleDescribable;
812
import com.rundeck.plugins.ansible.ansible.AnsibleRunnerContextBuilder;
913
import com.rundeck.plugins.ansible.plugin.AnsibleNodeExecutor;
@@ -103,19 +107,27 @@ public static Map<String, String> getRuntimeProperties(ExecutionContext context,
103107
return filterProperties;
104108
}
105109

106-
107-
public static File createTemporaryFile(String suffix, String data) throws IOException {
108-
File tempVarsFile = File.createTempFile("ansible-runner", suffix);
110+
public static File createTemporaryFile(String prefix, String suffix, String data, String path) throws IOException {
111+
if(prefix.isEmpty()){
112+
prefix ="ansible-runner";
113+
}
114+
File tempVarsFile = File.createTempFile(prefix, suffix, new File(path));
109115
Files.write(tempVarsFile.toPath(), data.getBytes());
110116
return tempVarsFile;
111117
}
112118

113-
114119
public static String randomString(){
115120
byte[] bytes = new byte[32];
116121
new SecureRandom().nextBytes(bytes);
117122
return Base64.getEncoder().encodeToString(bytes);
123+
}
118124

125+
public static String getCustomTmpPathDir(Framework framework){
126+
String customTmpDir = framework.getPropertyLookup().getProperty("framework.tmp.dir");
127+
if (customTmpDir == null || customTmpDir.isEmpty()) {
128+
customTmpDir = System.getProperty("java.io.tmpdir");
129+
}
130+
return customTmpDir;
119131
}
120132

121133

0 commit comments

Comments
 (0)