Skip to content

Commit

Permalink
[FIXED JENKINS-48498] Add become and become-user options (#17)
Browse files Browse the repository at this point in the history
* [FIXED JENKINS-48498] Add become and become-user options since sudo is deprecated. Mention sudo deprecation in UI but leave in place for now for backwards compatibility
  • Loading branch information
michaelcresswell authored and escoem committed Jan 16, 2018
1 parent 2ed3719 commit b791370
Show file tree
Hide file tree
Showing 23 changed files with 197 additions and 18 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -31,6 +31,8 @@ steps {
skippedTags(String tags)
startAtTask(String task)
credentialsId(String id)
become(boolean become = true)
becomeUser(String user = 'root')
sudo(boolean sudo = true)
sudoUser(String user = 'root')
forks(int forks = 5)
Expand All @@ -49,6 +51,8 @@ steps {
inventoryContent(String content, boolean dynamic = false)
credentialsId(String id)
hostPattern(String pattern)
become(boolean become = true)
becomeUser(String user = 'root')
sudo(boolean sudo = true)
sudoUser(String user = 'root')
forks(int forks = 5)
Expand All @@ -72,8 +76,8 @@ steps {
ansibleName('1.9.4')
tags('one,two')
credentialsId('credsid')
sudo(true)
sudoUser("user")
become(true)
becomeUser("user")
extraVars {
extraVar("key1", "value1", false)
extraVar("key2", "value2", true)
Expand Down
Expand Up @@ -48,6 +48,8 @@ abstract class AbstractAnsibleInvocation<T extends AbstractAnsibleInvocation<T>>

protected String exe;
protected int forks;
protected boolean become;
protected String becomeUser;
protected boolean sudo;
protected String sudoUser;
protected StandardCredentials vaultCredentials;
Expand Down Expand Up @@ -145,6 +147,20 @@ public ArgumentListBuilder appendAdditionalParameters(ArgumentListBuilder args)
return args;
}

public T setBecome(boolean become, String becomeUser) {
this.become = become;
this.becomeUser = becomeUser;
return (T) this;
}

protected ArgumentListBuilder appendBecome(ArgumentListBuilder args) {
if (become) {
args.add("-b");
addOptionAndValue(args, "--become-user", becomeUser);
}
return args;
}

public T setSudo(boolean sudo, String sudoUser) {
this.sudo = sudo;
this.sudoUser = sudoUser;
Expand Down
Expand Up @@ -65,6 +65,10 @@ public class AnsibleAdHocCommandBuilder extends Builder implements SimpleBuildSt

public final String command;

public boolean become = false;

public String becomeUser = "root";

public boolean sudo = false;

public String sudoUser = "root";
Expand Down Expand Up @@ -124,6 +128,15 @@ public void setCredentialsId(String credentialsId) {
public void setVaultCredentialsId(String vaultCredentialsId) {
this.vaultCredentialsId = vaultCredentialsId;
}

public void setBecome(boolean become) {
this.become = become;
}

@DataBoundSetter
public void setBecomeUser(String becomeUser) {
this.becomeUser = becomeUser;
}

@DataBoundSetter
public void setSudo(boolean sudo) {
Expand Down Expand Up @@ -180,6 +193,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath ws, @Nonnull Launc
invocation.setInventory(inventory);
invocation.setModule(module);
invocation.setModuleCommand(command);
invocation.setBecome(become, becomeUser);
invocation.setSudo(sudo, sudoUser);
invocation.setForks(forks);
invocation.setCredentials(StringUtils.isNotBlank(credentialsId) ?
Expand Down
Expand Up @@ -87,6 +87,7 @@ protected ArgumentListBuilder buildCommandLine()
appendInventory(args);
appendModule(args);
appendModuleCommand(args);
appendBecome(args);
appendSudo(args);
appendForks(args);
appendCredentials(args);
Expand Down
Expand Up @@ -68,6 +68,10 @@ public class AnsiblePlaybookBuilder extends Builder implements SimpleBuildStep

public String vaultCredentialsId = null;

public boolean become = false;

public String becomeUser = "root";

public boolean sudo = false;

public String sudoUser = "root";
Expand Down Expand Up @@ -155,6 +159,15 @@ public void setVaultCredentialsId(String vaultCredentialsId) {
this.vaultCredentialsId = vaultCredentialsId;
}

public void setBecome(boolean become) {
this.become = become;
}

@DataBoundSetter
public void setBecomeUser(String becomeUser) {
this.becomeUser = becomeUser;
}

@DataBoundSetter
public void setSudo(boolean sudo) {
this.sudo = sudo;
Expand Down Expand Up @@ -220,6 +233,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull Node node, @Nonnull FilePat
invocation.setTags(tags);
invocation.setSkippedTags(skippedTags);
invocation.setStartTask(startAtTask);
invocation.setBecome(become, becomeUser);
invocation.setSudo(sudo, sudoUser);
invocation.setForks(forks);
invocation.setCredentials(StringUtils.isNotBlank(credentialsId) ?
Expand Down
Expand Up @@ -109,6 +109,7 @@ protected ArgumentListBuilder buildCommandLine() throws InterruptedException, An
appendTags(args);
appendSkippedTags(args);
appendStartTask(args);
appendBecome(args);
appendSudo(args);
appendForks(args);
appendCredentials(args);
Expand Down
Expand Up @@ -31,6 +31,8 @@ public Object ansibleAdHoc(String module, String command, Runnable closure) {
adhoc.setColorizedOutput(context.isColorizedOutput());
adhoc.setForks(context.getForks());
adhoc.setHostKeyChecking(context.isHostKeyChecking());
adhoc.setBecome(context.isBecome());
adhoc.setBecomeUser(context.getBecomeUser());
adhoc.setSudo(context.isSudo());
adhoc.setSudoUser(context.getSudoUser());
adhoc.setUnbufferedOutput(context.isUnbufferedOutput());
Expand All @@ -53,6 +55,8 @@ public Object ansiblePlaybook(String playbook, Runnable closure) {
plbook.setColorizedOutput(context.isColorizedOutput());
plbook.setForks(context.getForks());
plbook.setHostKeyChecking(context.isHostKeyChecking());
plbook.setBecome(context.isBecome());
plbook.setBecomeUser(context.getBecomeUser());
plbook.setSudo(context.isSudo());
plbook.setSudoUser(context.getSudoUser());
plbook.setUnbufferedOutput(context.isUnbufferedOutput());
Expand Down
Expand Up @@ -23,6 +23,8 @@ public class AnsibleContext implements Context {
private String content;
private String input;
private String output;
private boolean become = false;
private String becomeUser = "root";
private boolean sudo = false;
private String sudoUser = "root";
private int forks = 5;
Expand Down Expand Up @@ -86,6 +88,14 @@ public void input(String input) {
public void output(String output) {
this.output = output;
}

public void become(boolean become) {
this.become = become;
}

public void becomeUser(String becomeUser) {
this.becomeUser = becomeUser;
}

public void sudo(boolean sudo) {
this.sudo = sudo;
Expand Down Expand Up @@ -175,6 +185,14 @@ public Inventory getInventory() {
return inventory;
}

public boolean isBecome() {
return become;
}

public String getBecomeUser() {
return becomeUser;
}

public boolean isSudo() {
return sudo;
}
Expand Down
Expand Up @@ -65,6 +65,8 @@ public class AnsiblePlaybookStep extends AbstractStepImpl {
private String installation;
private String credentialsId;
private String vaultCredentialsId;
private boolean become = false;
private String becomeUser = "root";
private boolean sudo = false;
private String sudoUser = "root";
private String limit = null;
Expand Down Expand Up @@ -107,6 +109,15 @@ public void setVaultCredentialsId(String vaultCredentialsId) {
this.vaultCredentialsId = Util.fixEmptyAndTrim(vaultCredentialsId);
}

public void setBecome(boolean become) {
this.become = become;
}

@DataBoundSetter
public void setBecomeUser(String becomeUser) {
this.becomeUser = Util.fixEmptyAndTrim(becomeUser);
}

@DataBoundSetter
public void setSudo(boolean sudo) {
this.sudo = sudo;
Expand Down Expand Up @@ -195,6 +206,14 @@ public String getVaultCredentialsId() {
return vaultCredentialsId;
}

public boolean isBecome() {
return become;
}

public String getBecomeUser() {
return becomeUser;
}

public boolean isSudo() {
return sudo;
}
Expand Down Expand Up @@ -342,6 +361,8 @@ protected Void run() throws Exception {
}
AnsiblePlaybookBuilder builder = new AnsiblePlaybookBuilder(step.getPlaybook(), inventory);
builder.setAnsibleName(step.getInstallation());
builder.setBecome(step.isBecome());
builder.setBecomeUser(step.getBecomeUser());
builder.setSudo(step.isSudo());
builder.setSudoUser(step.getSudoUser());
builder.setCredentialsId(step.getCredentialsId(), true);
Expand Down
Expand Up @@ -36,6 +36,12 @@
<c:select/>
</f:entry>

<f:optionalBlock name="become" title="${%become}" checked="${instance.become}" inline="true" help="${descriptor.getHelpFile('become')}">
<f:entry title="${%become user}" field="becomeUser">
<f:textbox />
</f:entry>
</f:optionalBlock>

<f:optionalBlock name="sudo" title="${%sudo}" checked="${instance.sudo}" inline="true" help="${descriptor.getHelpFile('sudo')}">
<f:entry title="${%sudo user}" field="sudoUser">
<f:textbox />
Expand Down
@@ -0,0 +1,3 @@
<div>
Run operations with become. It works only when the remote user is sudoer with <em>nopasswd</em> option.
</div>
@@ -0,0 +1,3 @@
<div>
Desired become user. "root" is used when this field is empty.
</div>
@@ -1,3 +1,3 @@
<div>
Run operations with sudo. It works only when the remote user is sudoer with <em>nopasswd</em> option.
</div>
Run operations with sudo. It works only when the remote user is sudoer with <em>nopasswd</em> option. Sudo has been deprecated in favor of become and will be removed in Ansible 2.6.
</div>
@@ -1,3 +1,3 @@
<div>
Desired sudo user. "root" is used when this field is empty.
</div>
Desired sudo user. "root" is used when this field is empty. Sudo has been deprecated in favor of become and will be removed in Ansible 2.6.
</div>
Expand Up @@ -32,12 +32,19 @@
<c:select/>
</f:entry>

<f:optionalBlock name="sudo" title="${%sudo}" checked="${instance.sudo}" inline="true" help="${descriptor.getHelpFile('sudo')}">
<f:entry title="${%sudo user}" field="sudoUser">
<f:optionalBlock name="become" title="${%become}" checked="${instance.become}" inline="true" help="${descriptor.getHelpFile('become')}">
<f:entry title="${%become user}" field="becomeUser">
<f:textbox />
</f:entry>
</f:optionalBlock>

<f:optionalBlock name="sudo" title="${%sudo}" checked="${instance.sudo}" inline="true" help="${descriptor.getHelpFile('sudo')}">
<f:entry title="${%sudo user}" field="sudoUser">
<f:textbox />
</f:entry>
</f:optionalBlock>


<f:advanced>
<f:entry title="${%Tags to run}" field="tags">
<f:textbox/>
Expand Down
@@ -0,0 +1,3 @@
<div>
Run operations with become. It works only when the remote user is sudoer with <em>nopasswd</em> option.
</div>
@@ -0,0 +1,3 @@
<div>
Desired become user. "root" is used when this field is empty.
</div>
@@ -1,3 +1,3 @@
<div>
Run operations with sudo. It works only when the remote user is sudoer with <em>nopasswd</em> option.
</div>
Run operations with sudo. It works only when the remote user is sudoer with <em>nopasswd</em> option. Sudo has been deprecated in favor of become and will be removed in Ansible 2.6.
</div>
@@ -1,3 +1,3 @@
<div>
Desired sudo user. "root" is used when this field is empty.
</div>
Desired sudo user. "root" is used when this field is empty. Sudo has been deprecated in favor of become and will be removed in Ansible 2.6.
</div>
Expand Up @@ -15,10 +15,16 @@
<f:entry field="vaultCredentialsId" title="Vault credentials">
<c:select/>
</f:entry>
<f:entry field="sudo" title="Use sudo">
<f:entry field="become" title="Use become">
<f:checkbox/>
</f:entry>
<f:entry field="sudoUser" title="Sudo username">
<f:entry field="becomeUser" title="Become username">
<f:textbox/>
</f:entry>
<f:entry field="sudo" title="Use sudo (deprecated)">
<f:checkbox/>
</f:entry>
<f:entry field="sudoUser" title="Sudo username (deprecated)">
<f:textbox/>
</f:entry>
<f:entry field="limit" title="Host subset">
Expand Down

0 comments on commit b791370

Please sign in to comment.