Skip to content

Commit

Permalink
Merge pull request #77 from yassineazzouz/master
Browse files Browse the repository at this point in the history
Issues Fixes
  • Loading branch information
yassineazzouz authored Oct 28, 2016
2 parents 54c9d54 + b7f605a commit eb987ec
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/batix/rundeck/core/AnsibleDescribable.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static String[] getValues() {
public static final String DEFAULT_ANSIBLE_EXECUTABLE = "/bin/sh";
public static final String ANSIBLE_GATHER_FACTS = "ansible-gather-facts";
public static final String ANSIBLE_IGNORE_ERRORS = "ansible-ignore-errors";
public static final String ANSIBLE_EXTRA_TAG = "ansible-extra-tag";
public static final String ANSIBLE_LIMIT = "ansible-limit";
public static final String ANSIBLE_IGNORE_TAGS = "ansible-ignore-tags-prefix";
public static final String ANSIBLE_EXTRA_VARS = "ansible-extra-vars";
Expand Down Expand Up @@ -171,6 +172,14 @@ public static String[] getValues() {
""
);

public static Property EXTRA_TAG_PROP = PropertyUtil.string(
ANSIBLE_EXTRA_TAG,
"Additional host tag",
"This tag will be added to all hosts discovered by this source.",
false,
""
);

static final Property EXTRA_VARS_PROP = PropertyBuilder.builder()
.string(ANSIBLE_EXTRA_VARS)
.required(false)
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/com/batix/rundeck/core/AnsibleRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@ public int run() throws Exception {
listener = ListenerFactory.getListener(System.out);
}

if (debug) {
System.out.println(" procArgs: " + procArgs);
}

if (extraParams != null && extraParams.length() > 0) {
procArgs.addAll(tokenizeCommand(extraParams));
}

if (debug) {
System.out.println(" procArgs: " + procArgs);
}

// execute the ansible process
ProcessBuilder processBuilder = new ProcessBuilder()
.command(procArgs)
Expand All @@ -430,28 +430,26 @@ public int run() throws Exception {

try {
proc = processBuilder.start();

OutputStream stdin = proc.getOutputStream();
OutputStreamWriter stdinw = new OutputStreamWriter(stdin);

if (sshUsePassword) {
if (sshPass != null && sshPass.length() > 0) {
OutputStream stdin = proc.getOutputStream();
OutputStreamWriter out = new OutputStreamWriter(stdin);
out.write(sshPass+"\n");
out.close();
stdinw.write(sshPass+"\n");
stdinw.flush();
} else {
throw new AnsibleException("Missing ssh password.",AnsibleException.AnsibleFailureReason.AnsibleNonZero);
}
}

if (become) {
if (becomePassword != null && becomePassword.length() > 0) {
OutputStream stdin = proc.getOutputStream();
OutputStreamWriter out = new OutputStreamWriter(stdin);
out.write(becomePassword+"\n");
out.close();
stdinw.write(becomePassword+"\n");
stdinw.flush();
}
}

proc.getOutputStream().close();
stdinw.close();
Thread errthread = Logging.copyStreamThread(proc.getErrorStream(), listener);
Thread outthread = Logging.copyStreamThread(proc.getInputStream(), listener);
errthread.start();
Expand Down Expand Up @@ -483,6 +481,7 @@ public int run() throws Exception {
// Make sure to always cleanup on failure and success
proc.getErrorStream().close();
proc.getInputStream().close();
proc.getOutputStream().close();
proc.destroy();

if (tempFile != null && !tempFile.delete()) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/batix/rundeck/core/AnsibleRunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,23 @@ public Boolean getBecome() {
return become;
}

public String getExtraParams() {
final String extraParams;
extraParams = PropertyResolver.resolveProperty(
AnsibleDescribable.ANSIBLE_EXTRA_PARAM,
null,
getFrameworkProject(),
getFramework(),
getNode(),
getjobConf()
);

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

public BecomeMethodType getBecomeMethod() {
String becomeMethod = PropertyResolver.resolveProperty(
AnsibleDescribable.ANSIBLE_BECOME_METHOD,
Expand Down Expand Up @@ -726,6 +743,11 @@ public AnsibleRunner buildAnsibleRunner() throws ConfigurationException{
}
}

String extraParams = getExtraParams();
if (extraParams != null) {
runner = runner.extraParams(extraParams);
}

String extraVars = getExtraVars();
if (extraVars != null) {
runner = runner.extraVars(extraVars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class AnsibleResourceModelSource implements ResourceModelSource {
private boolean ignoreErrors = false;
private String limit;
private String ignoreTagPrefix;
private String extraTag;

protected String vaultPass;
protected Boolean debug = false;
Expand Down Expand Up @@ -94,6 +95,8 @@ public void configure(Properties configuration) throws ConfigurationException {
limit = (String) resolveProperty(AnsibleDescribable.ANSIBLE_LIMIT,null,configuration,executionDataContext);
ignoreTagPrefix = (String) resolveProperty(AnsibleDescribable.ANSIBLE_IGNORE_TAGS,null,configuration,executionDataContext);

extraTag = (String) resolveProperty(AnsibleDescribable.ANSIBLE_EXTRA_TAG,null,configuration,executionDataContext);

sshAuthType = resolveProperty(AnsibleDescribable.ANSIBLE_SSH_AUTH_TYPE,AuthenticationType.privateKey.name(),configuration,executionDataContext);

sshUser = (String) resolveProperty(AnsibleDescribable.ANSIBLE_SSH_USER,null,configuration,executionDataContext);
Expand Down Expand Up @@ -262,6 +265,9 @@ public INodeSet getNodes() throws ResourceModelSourceException {
if (ignoreTagPrefix != null && ignoreTagPrefix.length() > 0 && ele.getAsString().startsWith(ignoreTagPrefix)) continue;
tags.add(ele.getAsString());
}
if (extraTag != null && extraTag.length() > 0) {
tags.add(extraTag);
}
node.setTags(tags);

if (root.has("ansible_lsb")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public AnsibleResourceModelSourceFactory(final Framework framework) {
builder.property(IGNORE_ERRORS_PROP);
builder.property(LIMIT_PROP);
builder.property(IGNORE_TAGS_PREFIX_PROP);
builder.property(EXTRA_TAG_PROP);
builder.property(SSH_AUTH_TYPE_PROP);
builder.property(SSH_USER_PROP);
builder.property(SSH_PASSWORD_PROP);
Expand Down

0 comments on commit eb987ec

Please sign in to comment.