Skip to content

Commit

Permalink
Merge pull request #148 from ltamaster/set-ansible-config-path
Browse files Browse the repository at this point in the history
Set ansible config path per Project on Resource Model, Node Executor and File Copier
  • Loading branch information
DerekTBrown authored Sep 27, 2017
2 parents 7cce6a0 + 913f1b6 commit fe684d9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/com/batix/rundeck/core/AnsibleDescribable.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public static String[] getValues() {
public static final String ANSIBLE_BECOME_PASSWORD_STORAGE_PATH = "ansible-become-password-storage-path";
public static final String DEFAULT_ANSIBLE_BECOME_PASSWORD_OPTION = "ansible-become-password";


public static final String ANSIBLE_CONFIG_FILE_PATH = "ansible-config-file-path";

public static final String PROJ_PROP_PREFIX = "project.";
public static final String FWK_PROP_PREFIX = "framework.";

Expand Down Expand Up @@ -166,7 +169,7 @@ public static String[] getValues() {
);

public static Property INVENTORY_PROP = PropertyUtil.string(ANSIBLE_INVENTORY, "ansible inventory File path",
"File Path to the ansible inventory to use", false, null);
"File Path to the ansible inventory to use, It can be blank if \"Ansible config file path\" is set ", false, null);

public static Property EXECUTABLE_PROP = PropertyUtil.freeSelect(
ANSIBLE_EXECUTABLE,
Expand Down Expand Up @@ -397,4 +400,11 @@ public static String[] getValues() {
.renderingOption(StringRenderingConstants.GROUP_NAME,"Privilege Escalation")
.build();

static final Property CONFIG_FILE_PATH = PropertyBuilder.builder()
.string(ANSIBLE_CONFIG_FILE_PATH)
.required(false)
.title("Ansible config file path")
.description("Set ansible config file path.")
.build();

}
17 changes: 17 additions & 0 deletions src/main/java/com/batix/rundeck/core/AnsibleRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public static List<String> tokenizeCommand(String commandline) {
private int result;
private Map<String, String> options = new HashMap<>();

protected String configFile;

private Listener listener;

private AnsibleRunner(AnsibleCommand type) {
Expand Down Expand Up @@ -232,6 +234,13 @@ public AnsibleRunner becomePassword(String pass) {
return this;
}

public AnsibleRunner configFile(String path) {
if (path != null && path.length() > 0) {
configFile = path;
}
return this;
}

/**
* Set the listener to notify, when run in stream mode, see {@link #stream()}
* @param listener the listener which will receive output lines
Expand Down Expand Up @@ -435,6 +444,14 @@ public int run() throws Exception {

Map<String, String> processEnvironment = processBuilder.environment();

if (configFile != null && configFile.length() > 0) {
if (debug) {
System.out.println(" ANSIBLE_CONFIG: "+configFile);
}

processEnvironment.put("ANSIBLE_CONFIG", configFile);
}

for (String optionName : this.options.keySet()) {
processEnvironment.put(optionName, this.options.get(optionName));
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/batix/rundeck/core/AnsibleRunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,25 @@ public String getLimit() {
}
return limit;
}

public String getConfigFile() {

final String configFile;
configFile = PropertyResolver.resolveProperty(
AnsibleDescribable.ANSIBLE_CONFIG_FILE_PATH,
null,
getFrameworkProject(),
getFramework(),
getNode(),
getjobConf()
);

if (null != configFile && configFile.contains("${")) {
return DataContextUtils.replaceDataReferences(configFile, getContext().getDataContext());
}
return configFile;
}


public AnsibleRunner buildAnsibleRunner() throws ConfigurationException{

Expand Down Expand Up @@ -817,6 +836,12 @@ public AnsibleRunner buildAnsibleRunner() throws ConfigurationException{
runner = runner.becomePassword(become_password);
}


String configFile = getConfigFile();
if (configFile != null) {
runner = runner.configFile(configFile);
}

return runner;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class AnsibleFileCopier implements FileCopier, AnsibleDescribable {
builder.name(SERVICE_PROVIDER_NAME);
builder.title("Ansible File Copier");
builder.description("Sends a file to a node via the copy module.");
builder.property(CONFIG_FILE_PATH);
builder.property(SSH_AUTH_TYPE_PROP);
builder.property(SSH_USER_PROP);
builder.property(SSH_PASSWORD_STORAGE_PROP);
Expand All @@ -43,6 +44,8 @@ public class AnsibleFileCopier implements FileCopier, AnsibleDescribable {
builder.property(BECOME_AUTH_TYPE_PROP);
builder.property(BECOME_USER_PROP);
builder.property(BECOME_PASSWORD_STORAGE_PROP);
builder.mapping(ANSIBLE_CONFIG_FILE_PATH,PROJ_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);
builder.frameworkMapping(ANSIBLE_CONFIG_FILE_PATH,FWK_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);
DESC=builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class AnsibleNodeExecutor implements NodeExecutor, AnsibleDescribable {
builder.description("Runs Ansible Ad-Hoc commands on the nodes using the shell module.");
builder.property(EXECUTABLE_PROP);
builder.property(WINDOWS_EXECUTABLE_PROP);
builder.property(CONFIG_FILE_PATH);
builder.property(SSH_AUTH_TYPE_PROP);
builder.property(SSH_USER_PROP);
builder.property(SSH_PASSWORD_STORAGE_PROP);
Expand All @@ -47,6 +48,8 @@ public class AnsibleNodeExecutor implements NodeExecutor, AnsibleDescribable {
builder.frameworkMapping(ANSIBLE_EXECUTABLE,FWK_PROP_PREFIX + ANSIBLE_EXECUTABLE);
builder.mapping(ANSIBLE_WINDOWS_EXECUTABLE,PROJ_PROP_PREFIX + ANSIBLE_WINDOWS_EXECUTABLE);
builder.frameworkMapping(ANSIBLE_WINDOWS_EXECUTABLE,FWK_PROP_PREFIX + ANSIBLE_WINDOWS_EXECUTABLE);
builder.mapping(ANSIBLE_CONFIG_FILE_PATH,PROJ_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);
builder.frameworkMapping(ANSIBLE_CONFIG_FILE_PATH,FWK_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);
builder.mapping(ANSIBLE_SSH_AUTH_TYPE,PROJ_PROP_PREFIX + ANSIBLE_SSH_AUTH_TYPE);
builder.frameworkMapping(ANSIBLE_SSH_AUTH_TYPE,FWK_PROP_PREFIX + ANSIBLE_SSH_AUTH_TYPE);
builder.mapping(ANSIBLE_SSH_USER,PROJ_PROP_PREFIX + ANSIBLE_SSH_USER);
Expand Down Expand Up @@ -133,6 +136,7 @@ public NodeExecutorResult executeCommand(ExecutionContext context, String[] comm
jobConf.put(AnsibleDescribable.ANSIBLE_DEBUG,"False");
}


AnsibleRunnerBuilder builder = new AnsibleRunnerBuilder(node, context, context.getFramework(), jobConf);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class AnsibleResourceModelSource implements ResourceModelSource {
protected String becomeMethod;
protected String becomeUser;
protected String becomePassword;
protected String configFile;

public AnsibleResourceModelSource(final Framework framework) {
this.framework = framework;
Expand Down Expand Up @@ -119,6 +120,10 @@ public void configure(Properties configuration) throws ConfigurationException {
becomeMethod = (String) resolveProperty(AnsibleDescribable.ANSIBLE_BECOME_METHOD,null,configuration,executionDataContext);
becomeUser = (String) resolveProperty(AnsibleDescribable.ANSIBLE_BECOME_USER,null,configuration,executionDataContext);
becomePassword = (String) resolveProperty(AnsibleDescribable.ANSIBLE_BECOME_PASSWORD,null,configuration,executionDataContext);


configFile = (String) resolveProperty(AnsibleDescribable.ANSIBLE_CONFIG_FILE_PATH,null,configuration,executionDataContext);

}

public AnsibleRunner buildAnsibleRunner() throws ResourceModelSourceException{
Expand Down Expand Up @@ -183,6 +188,10 @@ public AnsibleRunner buildAnsibleRunner() throws ResourceModelSourceException{
runner = runner.becomePassword(becomePassword);
}

if (configFile != null) {
runner = runner.configFile(configFile);
}

return runner;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public AnsibleResourceModelSourceFactory(final Framework framework) {
builder.description("Imports nodes from Ansible's inventory.");

builder.property(INVENTORY_PROP);
builder.property(CONFIG_FILE_PATH);
builder.property(GATHER_FACTS_PROP);
builder.property(IGNORE_ERRORS_PROP);
builder.property(LIMIT_PROP);
Expand All @@ -48,6 +49,10 @@ public AnsibleResourceModelSourceFactory(final Framework framework) {
builder.property(BECOME_PASSWORD_PROP);
builder.mapping(ANSIBLE_INVENTORY,PROJ_PROP_PREFIX + ANSIBLE_INVENTORY);
builder.frameworkMapping(ANSIBLE_INVENTORY,FWK_PROP_PREFIX + ANSIBLE_INVENTORY);
builder.mapping(ANSIBLE_CONFIG_FILE_PATH,PROJ_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);
builder.frameworkMapping(ANSIBLE_CONFIG_FILE_PATH,FWK_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);



DESC=builder.build();
}
Expand Down

0 comments on commit fe684d9

Please sign in to comment.