Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPL-69-unable-to-modify-ansible-tmp-dir #402

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN apt-get -y install sshpass && \
apt-get -y install sudo && \
pip3 install --upgrade pip

RUN pip3 install ansible==9.6.0
RUN pip3 install ansible==9.6.1

RUN ln -s /usr/bin/python3 /usr/bin/python

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException;
import com.rundeck.plugins.ansible.util.AnsibleUtil;

import java.io.File;
import java.io.PrintWriter;
Expand All @@ -18,7 +19,7 @@ public AnsibleInlineInventoryBuilder(String inline_inventory) {

public File buildInventory() throws ConfigurationException {
try {
File file = File.createTempFile("ansible-inventory", ".inventory");
File file = AnsibleUtil.createTemporaryFile("ansible-inventory", ".inventory","");
file.deleteOnExit();
PrintWriter writer = new PrintWriter(file);
writer.write(inline_inventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;

import com.google.gson.Gson;
import com.rundeck.plugins.ansible.util.AnsibleUtil;

public class AnsibleInventoryBuilder {

Expand All @@ -21,7 +22,7 @@ public AnsibleInventoryBuilder(Collection<INodeEntry> nodes) {

public File buildInventory() throws ConfigurationException {
try {
File file = File.createTempFile("ansible-inventory", ".json");
File file = AnsibleUtil.createTemporaryFile("ansible-inventory", ".json","");
file.deleteOnExit();
PrintWriter writer = new PrintWriter(file);
AnsibleInventory ai = new AnsibleInventory();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rundeck.plugins.ansible.ansible;

import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException;
import com.dtolabs.rundeck.core.utils.Utility;
import com.fasterxml.jackson.core.type.TypeReference;
import com.rundeck.plugins.ansible.util.*;
import com.dtolabs.rundeck.core.utils.SSHAgentProcess;
Expand All @@ -14,6 +15,7 @@
import java.nio.file.*;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.*;

@Builder
Expand Down Expand Up @@ -531,7 +533,7 @@ public int run() throws Exception {
List<VaultPrompt> stdinVariables = new ArrayList<>();

if(useAnsibleVault || vaultPass != null ){
vaultPromptFile = File.createTempFile("vault-prompt", ".log");
vaultPromptFile = AnsibleUtil.createTemporaryFile("vault-prompt",".log", "");
}

if (useAnsibleVault) {
Expand Down Expand Up @@ -677,7 +679,7 @@ public boolean registerKeySshAgent(String keyPath) throws Exception {

File tempPassVarsFile = null;
if (sshPassphrase != null && sshPassphrase.length() > 0) {
tempPassVarsFile = File.createTempFile("ansible-runner", "ssh-add-check");
tempPassVarsFile = AnsibleUtil.createTemporaryFile("ssh-add-check", "");
tempPassVarsFile.setExecutable(true);

List<String> passScript = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.*;
import com.rundeck.plugins.ansible.ansible.AnsibleException;

import static java.io.File.createTempFile;

@Data
@Builder
public class AnsibleVault {
Expand Down Expand Up @@ -129,7 +131,7 @@ public String encryptVariable(String key,


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

try {
Files.copy(AnsibleUtil.class.getClassLoader().getResourceAsStream("vault-client.py"),
Expand All @@ -138,7 +140,6 @@ public static File createVaultScriptAuth(String suffix) throws IOException {
} catch (IOException e) {
throw new IOException("Failed to copy vault-client.py", e);
}

Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-xr-x");
Files.setPosixFilePermissions(tempInternalVaultFile.toPath(), perms);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.rundeck.plugins.ansible.ansible.AnsibleInventoryList;
import com.rundeck.plugins.ansible.ansible.AnsibleRunner;
import com.rundeck.plugins.ansible.ansible.InventoryList;
import com.rundeck.plugins.ansible.util.AnsibleUtil;
import com.rundeck.plugins.ansible.util.VaultPrompt;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -41,13 +42,11 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -420,7 +419,7 @@ public void processWithGatherFacts(NodeSetImpl nodes, AnsibleRunner.AnsibleRunne
final Gson gson = new Gson();
Path tempDirectory;
try {
tempDirectory = Files.createTempDirectory("ansible-hosts");
tempDirectory = Files.createTempDirectory(Paths.get(AnsibleUtil.getAnsibleTmpPath()), "ansible-hosts");
} catch (IOException e) {
throw new ResourceModelSourceException("Error creating temporary directory: " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

public class AnsibleUtil {

public static final String RUNNER_OVERRIDE_TMP_DIR = "runner.rundeck.overrideTempDir";
public static final String RUNNER_DIRS_TMP = "runner.dirs.tmp" ;
public static final String ANSIBLE_CUSTOM_TMP_DIR = "ansible.custom.tmp.dir";
public static final String DEFAULT_TMP_DIR = "java.io.tmpdir";

public static SecretBundle createBundle(AnsibleRunnerContextBuilder builder){

DefaultSecretBundle secretBundle = new DefaultSecretBundle();
Expand Down Expand Up @@ -105,11 +110,30 @@ public static Map<String, String> getRuntimeProperties(ExecutionContext context,


public static File createTemporaryFile(String suffix, String data) throws IOException {
File tempVarsFile = File.createTempFile("ansible-runner", suffix);
return createTemporaryFile("ansible-runner", suffix, data);
}

public static File createTemporaryFile(String prefix, String suffix, String data) throws IOException {
return createTemporaryFile(prefix, suffix, data, new File(getAnsibleTmpPath()));
}

public static File createTemporaryFile( String prefix, String suffix, String data, File path) throws IOException {
File tempVarsFile = File.createTempFile(prefix, suffix, path);
Files.write(tempVarsFile.toPath(), data.getBytes());
return tempVarsFile;
}

public static String getAnsibleTmpPath() {
if( Boolean.getBoolean(System.getProperty(RUNNER_OVERRIDE_TMP_DIR,"false"))
&& !System.getProperty(RUNNER_DIRS_TMP,"").isEmpty()){
return System.getProperty(RUNNER_DIRS_TMP);
}
if( !Boolean.getBoolean(System.getProperty(RUNNER_OVERRIDE_TMP_DIR,"false"))
&& !System.getProperty(ANSIBLE_CUSTOM_TMP_DIR,"").isEmpty()){
return System.getProperty(ANSIBLE_CUSTOM_TMP_DIR);
}
return System.getProperty(DEFAULT_TMP_DIR);
}

public static String randomString(){
byte[] bytes = new byte[32];
Expand Down
Loading