diff --git a/src/main/java/com/batix/rundeck/core/AnsibleDescribable.java b/src/main/java/com/batix/rundeck/core/AnsibleDescribable.java index adc6b3b5..f95b2a60 100644 --- a/src/main/java/com/batix/rundeck/core/AnsibleDescribable.java +++ b/src/main/java/com/batix/rundeck/core/AnsibleDescribable.java @@ -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"; @@ -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) diff --git a/src/main/java/com/batix/rundeck/core/AnsibleRunner.java b/src/main/java/com/batix/rundeck/core/AnsibleRunner.java index ebb00b47..68b67292 100644 --- a/src/main/java/com/batix/rundeck/core/AnsibleRunner.java +++ b/src/main/java/com/batix/rundeck/core/AnsibleRunner.java @@ -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) @@ -430,13 +430,13 @@ 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); } @@ -444,14 +444,12 @@ public int run() throws Exception { 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(); @@ -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()) { diff --git a/src/main/java/com/batix/rundeck/core/AnsibleRunnerBuilder.java b/src/main/java/com/batix/rundeck/core/AnsibleRunnerBuilder.java index fd3aa09f..92e1ea64 100644 --- a/src/main/java/com/batix/rundeck/core/AnsibleRunnerBuilder.java +++ b/src/main/java/com/batix/rundeck/core/AnsibleRunnerBuilder.java @@ -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, @@ -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); diff --git a/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSource.java b/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSource.java index 7d98e54b..ad53e52a 100644 --- a/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSource.java +++ b/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSource.java @@ -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; @@ -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); @@ -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")) { diff --git a/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSourceFactory.java b/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSourceFactory.java index 1d73046f..9d6c7ec0 100644 --- a/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSourceFactory.java +++ b/src/main/java/com/batix/rundeck/plugins/AnsibleResourceModelSourceFactory.java @@ -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);