Skip to content

Commit

Permalink
Merge pull request #167 from jonesbusy/feature/support-check-mode
Browse files Browse the repository at this point in the history
Add support for check mode
  • Loading branch information
jonesbusy authored Dec 21, 2023
2 parents 29a7f9b + 34ad90a commit ccb73a9
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ steps {
skippedTags(String tags)
startAtTask(String task)
credentialsId(String id)
checkMode(boolean checkMode = false)
become(boolean become = true)
becomeUser(String user = 'root')
sudo(boolean sudo = true)
Expand Down Expand Up @@ -477,6 +478,7 @@ steps {
credentialsId('credsid')
become(true)
becomeUser("user")
checkMode(false)
extraVars {
extraVar("key1", "value1", false)
extraVar("key2", "value2", true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class AbstractAnsibleInvocation<T extends AbstractAnsibleInvocation<T>>
protected boolean sudo;
protected String sudoUser;
protected StandardCredentials vaultCredentials;
protected FilePath vaultTmpPath;
protected FilePath vaultTmpPath = null;
protected StandardUsernameCredentials credentials;
protected List<ExtraVar> extraVars;
protected String additionalParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class AnsiblePlaybookBuilder extends Builder implements SimpleBuildStep {

public String becomeUser = "root";

public boolean checkMode = false;

public boolean sudo = false;

public String sudoUser = "root";
Expand Down Expand Up @@ -196,6 +198,11 @@ public void setSudo(boolean sudo) {
this.sudo = sudo;
}

@DataBoundSetter
public void setCheckMode(boolean checkMode) {
this.checkMode = checkMode;
}

@DataBoundSetter
public void setSudoUser(String sudoUser) {
this.sudoUser = sudoUser;
Expand Down Expand Up @@ -269,6 +276,7 @@ public void perform(
invocation.setSkippedTags(skippedTags);
invocation.setStartTask(startAtTask);
invocation.setBecome(become, becomeUser);
invocation.setCheckMode(checkMode);
invocation.setSudo(sudo, sudoUser);
invocation.setForks(forks);
invocation.setCredentials(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class AnsiblePlaybookInvocation extends AbstractAnsibleInvocation<Ansible
private String tags;
private String skippedTags;
private String startAtTask;
private boolean checkMode;

protected AnsiblePlaybookInvocation(String exe, AbstractBuild<?, ?> build, BuildListener listener, EnvVars envVars)
throws IOException, InterruptedException, AnsibleInvocationException {
Expand All @@ -50,6 +51,11 @@ public AnsiblePlaybookInvocation setPlaybook(String playbook) {
return this;
}

public AnsiblePlaybookInvocation setCheckMode(boolean checkMode) {
this.checkMode = checkMode;
return this;
}

private ArgumentListBuilder appendPlaybook(ArgumentListBuilder args) {
args.add(envVars.expand(playbook));
return args;
Expand Down Expand Up @@ -95,6 +101,13 @@ private ArgumentListBuilder appendStartTask(ArgumentListBuilder args) {
return args;
}

protected ArgumentListBuilder appendCheckMode(ArgumentListBuilder args) {
if (checkMode) {
args.add("--check");
}
return args;
}

@Override
protected ArgumentListBuilder buildCommandLine()
throws InterruptedException, AnsibleInvocationException, IOException {
Expand All @@ -108,6 +121,7 @@ protected ArgumentListBuilder buildCommandLine()
appendSkippedTags(args);
appendStartTask(args);
appendBecome(args);
appendCheckMode(args);
appendSudo(args);
appendForks(args);
appendCredentials(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Object ansiblePlaybook(String playbook, Runnable closure) {
plbook.setDisableHostKeyChecking(context.isDisableHostKeyChecking());
plbook.setBecome(context.isBecome());
plbook.setBecomeUser(context.getBecomeUser());
plbook.setCheckMode(context.isCheckMode());
plbook.setSudo(context.isSudo());
plbook.setSudoUser(context.getSudoUser());
plbook.setUnbufferedOutput(context.isUnbufferedOutput());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AnsibleContext implements Context {
private boolean sudo = false;
private String sudoUser = "root";
private int forks = 5;
private boolean checkMode = false;
private boolean unbufferedOutput = true;
private boolean colorizedOutput = false;
private boolean disableHostKeyChecking = false;
Expand Down Expand Up @@ -107,6 +108,10 @@ public void becomeUser(String becomeUser) {
this.becomeUser = becomeUser;
}

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

public void sudo(boolean sudo) {
this.sudo = sudo;
}
Expand Down Expand Up @@ -227,6 +232,10 @@ public boolean isColorizedOutput() {
return colorizedOutput;
}

public boolean isCheckMode() {
return checkMode;
}

public boolean isDisableHostKeyChecking() {
return disableHostKeyChecking;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class AnsiblePlaybookStep extends AbstractStepImpl {
private String vaultCredentialsId;
private String vaultTmpPath = null;
private boolean become = false;
private boolean checkMode = false;
private String becomeUser = "root";
private boolean sudo = false;
private String sudoUser = "root";
Expand Down Expand Up @@ -133,6 +134,11 @@ public void setBecomeUser(String becomeUser) {
this.becomeUser = Util.fixEmptyAndTrim(becomeUser);
}

@DataBoundSetter
public void setCheckMode(boolean checkMode) {
this.checkMode = checkMode;
}

@DataBoundSetter
public void setSudo(boolean sudo) {
this.sudo = sudo;
Expand Down Expand Up @@ -237,6 +243,10 @@ public String getBecomeUser() {
return becomeUser;
}

public boolean isCheckMode() {
return checkMode;
}

public boolean isSudo() {
return sudo;
}
Expand Down Expand Up @@ -433,6 +443,7 @@ protected Void run() throws Exception {
builder.setAnsibleName(step.getInstallation());
builder.setBecome(step.isBecome());
builder.setBecomeUser(step.getBecomeUser());
builder.setCheckMode(step.isCheckMode());
builder.setSudo(step.isSudo());
builder.setSudoUser(step.getSudoUser());
builder.setCredentialsId(step.getCredentialsId(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
</f:entry>
</f:optionalBlock>

<f:entry title="${%check mode}" field="checkMode">
<f:checkbox default="false" />
</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:textbox />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Run ansible with check mode enabled. This will not make any changes on the remote system (--check)
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<f:entry field="vaultTmpPath" title="Vault tmp path">
<f:textbox/>
</f:entry>
<f:entry field="checkMode" title="Check mode">
<f:checkbox/>
</f:entry>
<f:entry field="become" title="Use become">
<f:checkbox/>
</f:entry>
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/jenkinsci/plugins/ansible/PipelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public void testMinimalPipeline() throws Exception {
assertThat(run1.getLog(), allOf(containsString("ansible-playbook playbook.yml")));
}

@Test
public void testMinimalCheckModePipeline() throws Exception {
String pipeline = IOUtils.toString(
PipelineTest.class.getResourceAsStream("/pipelines/minimalCheckMode.groovy"), StandardCharsets.UTF_8);
WorkflowJob workflowJob = jenkins.createProject(WorkflowJob.class);
workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true));
WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart();
jenkins.waitForCompletion(run1);
assertThat(run1.getLog(), allOf(containsString("ansible-playbook playbook.yml --check")));
}

@Test
public void testExtraVarsHiddenString() throws Exception {
String pipeline = IOUtils.toString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
public class JobDslIntegrationTest {
public static final String ANSIBLE_DSL_GROOVY_PLAYBOOK = "jobdsl/playbook.groovy";
public static final String ANSIBLE_DSL_GROOVY_EXPANDER = "jobdsl/expander.groovy";
public static final String ANSIBLE_DSL_GROOVY_CHECK_MODE = "jobdsl/checkMode.groovy";
public static final String ANSIBLE_DSL_GROOVY_SECURITY_630 = "jobdsl/security630.groovy";
public static final String ANSIBLE_DSL_GROOVY_PLAYBOOK_LEGACY = "jobdsl/legacyPlaybook.groovy";
public static final String ANSIBLE_DSL_GROOVY_ADHOC = "jobdsl/adhoc.groovy";
Expand Down Expand Up @@ -76,6 +77,7 @@ public void shouldCreateJobWithPlaybookDsl() throws Exception {
assertThat("credentialsId", step.credentialsId, is("credsid"));
assertThat("become", step.become, is(true));
assertThat("becomeUser", step.becomeUser, is("user"));
assertThat("checkMode", step.checkMode, is(false));
assertThat("sudo", step.sudo, is(false));
assertThat("sudoUser", step.sudoUser, is("root"));
assertThat("forks", step.forks, is(6));
Expand All @@ -88,6 +90,18 @@ public void shouldCreateJobWithPlaybookDsl() throws Exception {
assertThat("extraVar.hidden", step.extraVars.get(0).isHidden(), is(true));
}

@Test
@DslJobRule.WithJobDsl(ANSIBLE_DSL_GROOVY_CHECK_MODE)
public void shouldCreateJobWithCheckMode() throws Exception {
AnsiblePlaybookBuilder step = dsl.getGeneratedJob().getBuildersList().get(AnsiblePlaybookBuilder.class);
assertThat("Should add playbook builder", step, notNullValue());

assertThat("playbook", step.playbook, is("path/playbook.yml"));
assertThat("inventory", step.inventory, (Matcher) isA(InventoryPath.class));
assertThat("ansibleName", step.ansibleName, is("1.9.4"));
assertThat("checkMode", step.checkMode, is(true));
}

@Test
@DslJobRule.WithJobDsl(ANSIBLE_DSL_GROOVY_EXPANDER)
public void shouldCreateJobWithVarExpander() throws Exception {
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/jobdsl/checkMode.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
freeStyleJob('ansible') {
steps {
ansiblePlaybook('path/playbook.yml') {
inventoryPath('hosts.ini')
ansibleName('1.9.4')
limit('retry.limit')
checkMode(true)
}
}
}
24 changes: 24 additions & 0 deletions src/test/resources/pipelines/minimalCheckMode.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pipeline {
agent {
label('test-agent')
}
stages {
stage('Create playbook') {
steps {
writeFile(encoding: 'UTF-8', file: 'playbook.yml', text: '''- hosts: localhost
connection: local
gather_facts: no
tasks:
- debug: msg=test
''')
}
}
stage('Ansible playbook') {
steps {
warnError(message: 'ansible command not found?') {
ansiblePlaybook(playbook: 'playbook.yml', checkMode: true)
}
}
}
}
}

0 comments on commit ccb73a9

Please sign in to comment.