• Stars
    star
    223
  • Rank 171,947 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Jenkins Ansible plugin

Jenkins Ansible Plugin

This plugin allows to execute Ansible tasks as a job build step.

Global Configuration

Ansible needs to be on the PATH for the build job in order to be used. This can be done through either Jenkins Global Tool Configuration or including Ansible on the OS User PATH variable.

Global Tool Configuration

Configuring Ansible through the Global Tool Configuration in Jenkins (Jenkins → Manage Jenkins → Global Tool Configuration) allows for multiple Ansible installations to be present and used by different Jenkins jobs.

  1. Click "Add Ansible"

  2. Configure the name and path

    Field name Description
    Name Symbolic name used to identify a specific Ansible installation when multiple installations are configured
    Path to ansible executables directory Directory containing the ansible, ansible-playbook, and ansible-vault binaries
  3. Repeat for any additional desired installations

OS User PATH

Ansible can also be added to the PATH user used by the Jenkins executor instead of configured through Global Tool Configuration. This is done through normal OS tools outside of Jenkins and is not covered by this guide.


Adhoc

Adhoc commands allow for simple operations to be done without writing a full playbook. This allows for a convenient way of doing quick tasks with Ansible.

Examples

Scripted

Jenkinsfile

ansibleAdhoc credentialsId: 'private_key', inventory: 'inventories/a/hosts', hosts: 'hosts_pattern', moduleArguments: 'module_arguments'

Declarative

Jenkinsfile

ansibleAdhoc(credentialsId: 'private_key', inventory: 'inventories/a/hosts', hosts: 'hosts_pattern', moduleArguments: 'module_arguments')

Arguments

See also jenkins.io documentation.

Freestyle Name Pipeline Name Description
Ansible installation installation Ansible installation to use for the playbook invocation
Host pattern hosts The host pattern to manage. See Ansible Patterns for details.
Module module CLI arg: -m
Module arguments or command to execute moduleArguments CLI arg: -a
Inventory file or host list inventory CLI arg: -i: See the Inventory section for additional details.
Inventory inline content inventoryContent CLI arg: -i: See the Inventory section for additional details.
Credentials credentialsId The Jenkins credential to use for the SSH connection. See the Authentication section for additional details.
Vault Credentials vaultCredentialsId CLI arg: --vault-password-file: The Jenkins credential to use as the vault credential. See the Vault Credentials section for additional details.
sudo become CLI arg: -s
sudo user becomeUser CLI arg: -U
Number of parallel processes forks CLI arg: -f
Check host SSH key hostKeyChecking Toggle checking of the host key. Sets the environment variable ANSIBLE_HOST_KEY_CHECKING, similar to the recommendations for running with Vagrant.
Unbuffered stdout Toggle buffering of standard out. Sets the environment variable PYTHONUNBUFFERED, similar to the recommendations for running with Vagrant.
Colorized stdout colorized Toggle color codes in console text. See Colorized Output section for example usage. Sets the environment variable ANSIBLE_FORCE_COLOR, similar to the recommendations for running with Vagrant.
Extra Variables extraVars CLI arg: -e
Additional parameters extras String passed to the Ansible Command Line invocation as-is.

Playbook

Ansible playbook operations can be run with the plugin. The plugin provides several conveniences such as easily using credentials from the Jenkins credential store, unbuffered color output in the log, etc.

Examples

Scripted

Jenkinsfile

ansiblePlaybook credentialsId: 'private_key', inventory: 'inventories/a/hosts', playbook: 'my_playbook.yml'

Declarative

Jenkinsfile

ansiblePlaybook(credentialsId: 'private_key', inventory: 'inventories/a/hosts', playbook: 'my_playbook.yml')

Additional scripted and declarative pipeline examples can be found on the plugin's GitHub readme.

Arguments

Refer to jenkins.io for documentation extracted from the online help of the plugin.

Freestyle Name Pipeline Name Description
Ansible installation installation Ansible installation to use for the playbook invocation
Playbook path playbook Mandatory. The name of the playbook to run
Inventory file or host list inventory CLI arg: -i: See the inventory section for details.
Inventory inline content inventoryContent CLI arg: -i: See the inventory section for details.
Credentials credentialsId The Jenkins credential to use for the SSH connection. See the Authentication section for additional details
Vault Credentials vaultCredentialsId The Jenkins credential to use as the vault credential. See the Vault Credentials section for more details
sudo sudo CLI arg: -s
sudo user sudoUser CLI arg: -U
Host subset limit CLI arg: -l
Tags to run tags CLI arg: -t
Tags to skip skippedTags CLI arg: --skip-tags
Task to start at startAtTask CLI arg: --start-at-task
Number of parallel processes forks CLI arg: -f
Check host SSH key hostKeyChecking Toggle checking of the host key. Sets the environment variable ANSIBLE_HOST_KEY_CHECKING, similar to the recommendations for running with Vagrant.
Colorized stdout colorized Toggle color codes in console text. See Colorized Output section for example usage. Sets the environment variable ANSIBLE_FORCE_COLOR, similar to the recommendations for running with Vagrant.
Additional parameters extras String passed to the Ansible Command Line invocation as-is
Extra Variables extraVars CLI arg: -e

Refer to the ansible-playbook manual page for details on how each command line argument is interpreted.

Authentication

SSH Keys

SSH keys are the recommended authentication method for SSH connections. The plugin supports the credential type "SSH Username with private key" configured in the Jenkins credential store through the SSH crendentials plugin.

Password

Even if using SSH keys is recommended authentication method, password authentication may sometimes be required. The plugin has supported password based authentication since 0.3.0. When using password based authentication, the sshpass binary is expected to be on the PATH. The plugin supports the credential type "Username with password" configured in the Jenkins credential store through the SSH crendentials plugin.

Vault Credentials

Vault credentials can be setup in the Jenkins credential store as either a "Secret text" or a "Secret file".

Colorized Output

The AnsiColor plugin is needed for colorized console output. Once installed, colorized output can be enabled with the argument "colorized: true".

Jenkinsfile

ansiColor('xterm') {
    ansiblePlaybook(
        playbook: 'path/to/playbook.yml',
        inventory: 'path/to/inventory.ini',
        credentialsId: 'sample-ssh-key',
        colorized: true)
}

Extra Parameters

Extra parameters is a string passed to the Ansible Command Line invocation as-is and can be useful for arguments occasionally added to an invocation at runtime, such as tags and host limits.

Inventory

File

A string path to the inventory file to use with the playbook invocation.

Inline

The provided content is used as the content of the inventory file for the playbook invocation.

Using Jenkins Environment Variables

Jenkins environment variables can be accessed from within an Ansible playbook. The Jenkins variables are injected as environment variables making them available through the Ansible lookup plugin.

The following Ansible playbook accesses the Jenkins BUILD_TAG variable:

playbook.yml

---
- hosts: example
  tasks:
    - debug: msg="{{ lookup('env','BUILD_TAG') }}"

Vault

Most Ansible Vault operations can be performed with the plugin. Interactive operations such as create, edit, and view are not supported through the plugin. One use case for this enabling developers to encrypt secret values while keeping the vault password a secret.

Examples

Scripted

Encrypts a File

ansibleVault action: 'encrypt', input: 'vars/secrets.yml', vaultCredentialsId: 'ansible_vault_credentials'

Encrypts a String

ansibleVault action: 'encrypt_string', content: 'secret_content', vaultCredentialsId: 'ansible_vault_credentials'

Declarative

Jenkinsfile

ansibleVault(action: 'encrypt', input: 'vars/secrets.yml', vaultCredentialsId: 'ansible_vault_credentials')

Jenkinsfile

ansibleVault(action: 'encrypt_string', content: 'secret_content', vaultCredentialsId: 'ansible_vault_password')

Arguments

See also jenkins.io Pipeline step documentation.

Freestyle Name Pipeline Name Description
Ansible installation installation Ansible installation to use for the playbook invocation
Action action Mandatory. The name of the action to use. Interactive operations such as create, edit, and view are not supported.
Vault Credentials vaultCredentialsId CLI arg: --vault-password-file. The Jenkins credential to use as the vault credential. See the Vault Credentials section for more details
New Vault Credentials newVaultCredentialsId CLI arg: --new-vault-password-file. The Jenkins credential to use as the vault credential. See the Vault Credentials section for more details
Content content The content to encrypt with the 'encrypt_string' action.
Input input The file to encrypt with the encrypt actions.
Output output CLI arg: --output

Vault Credentials

Vault credentials can be setup in the Jenkins credential store as either a "Secret text" or a "Secret file".


Open Issues

View issues in Jira


Changelog

Version 1.0 (26 March 2018)

  • Fix security issue: Do not disable host key verification by default. This may break existing configurations as host key verification will be enabled everywhere by default.

Version 0.8.0 (16 Jan 2018)

Version 0.6.2 (3 Jan 2017)

Version 0.6.1 (1 Jan 2017)

  • Use latest parent project definition in order to deploy plugin (thanks to alecharp for the help and the PR)

Version 0.6 (31 Dec 2016)

WARN: 0.6.x version will be the last one to support Jenkins 1.xxx and Ansible 1.x - The 0.7.x and next releases will require Jenkins 2.32.1 (or higher) and Ansible 2.2 (or higher)

  • Add a "do not specify" option for inventory JENKINS-34627
  • Support inventoryContent in pipeline (thanks to leewin12 for the PR)
  • Add support of extra variables in jobdsl (thanks to pawbur for the PR)
  • Support empty forks (number of parallel processes) parameter JENKINS-39438
  • Escape '%' character in private key path (thanks to ewollesen for the PR)
  • Omit ansible option when expanded environment variable is empty (thanks to vjestin for the PR)
  • Add the --forks parameter configurable in pipeline step (thanks to anguswilliams for the PR)
  • Fix usage of environment variable in ansiblePlaybook pipeline step (thanks to thomasKalmar and barthorre for the PR) JENKINS-38289

Version 0.5 (5 May 2016)

  • Add support for ansible extra variables JENKINS-29863
  • Improve Pipeline plugin integration JENKINS-32911
  • Add the possibility to use the default inventory file (thanks to Johann Schmitz for the PR)
  • Add colorized output in pipeline jobs (thanks to Kirill Merkushev for the PR)
  • Make Jenkins build variables available as environment variables for ansible (thanks to Kevin Mooney for the PR) JENKINS-29284

Version 0.4 (25 December 2015)

Version 0.3.1 (15 July 2015)

Version 0.3 (20 June 2015)

  • Add support for password based SSH authentication (with sshpass)
  • Environment variables can be used in Module and Module arguments text field in Ad-hoc command builder
  • Environment variables can be used in inline inventory text box JENKINS-28547

Version 0.2 (11 May 2015)

  • Fix NullPointerException when no credentials are selected
  • Fix --skippedTags parameter configuration which was ignored
  • Fix NullPointerException and print an error message in the build console when the inventory is not set in the job configuration

Version 0.1 (01 May 2015)

  • Initial version

This plugin gives the possibility to run Ansible ad-hoc command or playbooks as a build step.

Build Status

Using Jenkins Build and Environment Variables

It is possible to access build and environment variables in ansible playbooks. These variables are injected as environment variables within the ansible process. For example, use this code in an ansible playbook to access Jenkins BUILD_TAG variable.

---
- hosts: example
  tasks:
    - debug: msg="{{ lookup('env','BUILD_TAG') }}"

Job DSL support

steps {
    ansiblePlaybook(String playbook) {
        inventoryPath(String path)
        inventoryContent(String content, boolean dynamic = false)
        ansibleName(String name)
        limit(String limit)
        tags(String tags)
        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)
        unbufferedOutput(boolean unbufferedOutput = true)
        colorizedOutput(boolean colorizedOutput = false)
        hostKeyChecking(boolean hostKeyChecking = false)
        additionalParameters(String params)
        extraVars {
            extraVar(String key, String value, boolean hidden)
        }
    }

    ansibleAdHoc(String module, String command) {
        ansibleName(String name)
        inventoryPath(String path)
        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)
        unbufferedOutput(boolean unbufferedOutput = true)
        colorizedOutput(boolean colorizedOutput = false)
        hostKeyChecking(boolean hostKeyChecking = false)
        additionalParameters(String params)
        extraVars {
            extraVar(String key, String value, boolean hidden)
        }
    }
}

Example

steps {
    ansiblePlaybook('path/playbook.yml') {
        inventoryPath('hosts.ini')
        ansibleName('1.9.4')
        tags('one,two')
        credentialsId('credsid')
        become(true)
        becomeUser("user")
        extraVars {
            extraVar("key1", "value1", false)
            extraVar("key2", "value2", true)
        }
    }
}

Pipeline support

Ansible playbooks can be executed from workflow scripts. Only the playbook parameter is mandatory.

Example

node {
    ansiblePlaybook(
        playbook: 'path/to/playbook.yml',
        inventory: 'path/to/inventory.ini',
        credentialsId: 'sample-ssh-key',
        extras: '-e parameter="some value"')
}

Extra Variables

Extra variables can be passed to ansible by using a map in the pipeline script. Supported value types are: String, Boolean, Number. By default the value will be considered potentially sensitive and masked in the logs. To override this give a map with keys value and hidden.

node {
    ansiblePlaybook(
        inventory: 'local_inventory/hosts.cfg',
        playbook: 'cloud_playbooks/create-aws.yml',
        extraVars: [
            login: 'mylogin',
            toggle: true,
            forks: 8,
            not_secret: [value: 'I want to see this in the logs', hidden: false]
        ])
}

Colorized Console Log

You need to install the AnsiColor plugin to output a colorized Ansible log.

node {
    wrap([$class: 'AnsiColorBuildWrapper', colorMapName: "xterm"]) {
        ansiblePlaybook(
            playbook: 'path/to/playbook.yml',
            inventory: 'path/to/inventory.ini',
            credentialsId: 'sample-ssh-key',
            colorized: true)
    }
}

More Repositories

1

jenkins

Jenkins automation server
Java
21,381
star
2

docker

Docker official jenkins repo
Dockerfile
6,144
star
3

pipeline-examples

A collection of examples, tips and tricks and snippets of scripting for the Jenkins Pipeline plugin
Groovy
4,117
star
4

blueocean-plugin

Blue Ocean is a reboot of the Jenkins CI/CD User Experience
Java
2,872
star
5

configuration-as-code-plugin

Jenkins Configuration as Code Plugin
Java
2,521
star
6

kubernetes-plugin

Jenkins plugin to run dynamic agents in a Kubernetes/Docker environment
Java
2,206
star
7

job-dsl-plugin

A Groovy DSL for Jenkins Jobs - Sweeeeet!
Groovy
1,851
star
8

pipeline-plugin

Obsolete home for Pipeline plugins
1,711
star
9

JenkinsPipelineUnit

Framework for unit testing Jenkins pipelines
Groovy
1,426
star
10

gitlab-plugin

A Jenkins plugin for interfacing with GitLab
Java
1,418
star
11

jenkinsfile-runner

A command line tool to run Jenkinsfile as a function
Java
1,114
star
12

java-client-api

A Jenkins API client for Java
Java
888
star
13

jenkins-scripts

Scripts in Groovy, shell, Ruby, Python, whatever for managing/interacting with Jenkins
Groovy
880
star
14

build-monitor-plugin

Jenkins Build Monitor Plugin
Java
722
star
15

slack-plugin

A Jenkins plugin for posting notifications to a Slack channel
Java
664
star
16

git-plugin

Git repository access for Jenkins jobs
Java
660
star
17

pipeline-model-definition-plugin

Groovy
557
star
18

ghprb-plugin

github pull requests builder plugin for Jenkins
Java
495
star
19

docker-workflow-plugin

Jenkins plugin which allows building, testing, and using Docker images from Jenkins Pipeline projects.
Java
492
star
20

docker-plugin

Jenkins Cloud Plugin that uses Docker
Java
482
star
21

docker-inbound-agent

Docker image for a Jenkins agent which can connect to Jenkins using TCP or Websocket protocols
PowerShell
466
star
22

helm-charts

Jenkins helm charts
Mustache
448
star
23

pipeline-aws-plugin

Jenkins Pipeline Step Plugin for AWS
Java
423
star
24

jenkins.rb

Deprecated, see https://www.jenkins.io/jep/7
Ruby
394
star
25

generic-webhook-trigger-plugin

Can receive any HTTP request, extract any values from JSON or XML and trigger a job with those values available as variables. Works with GitHub, GitLab, Bitbucket, Jira and many more.
Java
377
star
26

email-ext-plugin

Jenkins Email Extension Plugin
Java
338
star
27

dingtalk-plugin

Dingtalk for jenkins
Java
336
star
28

warnings-ng-plugin

Jenkins Warnings Plugin - Next Generation
Java
323
star
29

plugin-installation-manager-tool

Plugin Manager CLI tool for Jenkins
Java
301
star
30

mesos-plugin

Mesos Cloud Jenkins Plugin
Java
291
star
31

github-plugin

Jenkins GitHub plugin
Java
286
star
32

ec2-plugin

Jenkins ec2 plugin
Java
282
star
33

ssh-steps-plugin

Jenkins pipeline steps which provides SSH facilities such as command execution or file transfer for continuous delivery.
Java
273
star
34

ansicolor-plugin

Jenkins ANSI Color Plugin
Java
253
star
35

pipeline-utility-steps-plugin

Small, miscellaneous, cross platform utility steps for Jenkins Pipeline jobs.
Java
237
star
36

docker-agent

Base Docker image for Jenkins Agents
PowerShell
231
star
37

workflow-cps-global-lib-plugin

Java
223
star
38

lib-file-leak-detector

Java agent that detects file handle leak
Java
217
star
39

hashicorp-vault-plugin

Jenkins plugin to populate environment variables from secrets stored in HashiCorp's Vault.
Java
214
star
40

bitbucket-branch-source-plugin

Bitbucket Branch Source Plugin
Java
213
star
41

remoting

Jenkins Remoting module
Java
212
star
42

workflow-aggregator-plugin

211
star
43

gerrit-trigger-plugin

Java
209
star
44

android-emulator-plugin

Android Emulator plugin for Jenkins
Java
207
star
45

docker-slaves-plugin

A Jenkins plugin to run builds inside Docker containers
Java
205
star
46

pipeline-stage-view-plugin

Visualizes Jenkins pipelines
JavaScript
204
star
47

jenkinsfile-runner-github-actions

Jenkins single-shot pipeline execution in a GitHub Action POC
Shell
199
star
48

github-branch-source-plugin

GitHub Branch Source Plugin
Java
193
star
49

amazon-ecs-plugin

Amazon EC2 Container Service Plugin for Jenkins
Java
193
star
50

trilead-ssh2

Patched trilead-ssh2 used in Jenkins
Java
193
star
51

cucumber-reports-plugin

Jenkins plugin to generate cucumber-jvm reports
Java
192
star
52

docker-build-publish-plugin

Java
192
star
53

performance-plugin

Performance Test Running and Reporting for Jenkins CI
Java
188
star
54

jira-plugin

Jenkins jira plugin
Java
168
star
55

gitea-plugin

This plugin provides the Jenkins integration for Gitea.
Java
168
star
56

embeddable-build-status-plugin

Embed build status of Jenkins jobs in web pages
Java
167
star
57

stashnotifier-plugin

A Jenkins Plugin to notify Atlassian Stash|Bitbucket of build results
Java
166
star
58

docker-ssh-agent

Docker image for Jenkins agents connected over SSH
PowerShell
162
star
59

workflow-cps-plugin

Java
160
star
60

http-request-plugin

This plugin does a request to an url with some parameters.
Java
154
star
61

stapler

Stapler web framework
Java
154
star
62

kubernetes-pipeline-plugin

Kubernetes Pipeline is Jenkins plugin which extends Jenkins Pipeline to provide native support for using Kubernetes pods, secrets and volumes to perform builds
Java
154
star
63

tfs-plugin

Jenkins tfs plugin
Java
145
star
64

jep

Jenkins Enhancement Proposals
Shell
144
star
65

kubernetes-cd-plugin

A Jenkins plugin to deploy to Kubernetes cluster
Java
140
star
66

jacoco-plugin

Jenkins JaCoCo Plugin
Java
139
star
67

qy-wechat-notification-plugin

企业微信Jenkins构建通知插件
Java
138
star
68

swarm-plugin

Jenkins swarm plugin
Java
133
star
69

git-client-plugin

Git client API for Jenkins plugins
Java
130
star
70

subversion-plugin

Jenkins subversion plugin
Java
126
star
71

dependency-check-plugin

Jenkins plugin for OWASP Dependency-Check. Inspects project components for known vulnerabilities (e.g. CVEs).
Java
125
star
72

role-strategy-plugin

Jenkins Role-Strategy plugin
Java
120
star
73

groovy-sandbox

(Deprecated) Compile-time transformer to run Groovy code in a restrictive sandbox
Java
120
star
74

jenkins-design-language

Styles, assets, and React classes for Jenkins Design Language
TypeScript
116
star
75

acceptance-test-harness

Acceptance tests cases for Jenkins and its plugins based on selenium and docker.
Java
116
star
76

scm-sync-configuration-plugin

Jenkins scm-sync-configuration plugin
Java
116
star
77

gitlab-branch-source-plugin

A Jenkins Plugin for GitLab Multibranch Pipeline jobs and Folder Organization
Java
115
star
78

git-parameter-plugin

Jenkins plugin for chosing Revision / Tag before build
Java
115
star
79

publish-over-ssh-plugin

Java
114
star
80

pipeline-as-yaml-plugin

Jenkins Pipeline As Yaml Plugin
Java
114
star
81

selenium-plugin

Jenkins selenium plugin
Java
112
star
82

docker-build-step-plugin

Java
111
star
83

code-coverage-api-plugin

Deprecated Jenkins Code Coverage Plugin
Java
111
star
84

jira-trigger-plugin

Triggers a build when a certain condition is matched in JIRA
Groovy
111
star
85

cobertura-plugin

Jenkins cobertura plugin
Java
110
star
86

gradle-plugin

Jenkins gradle plugin
Java
109
star
87

credentials-plugin

Provides Jenkins with extension points to securely store, manage, and bind credentials data to other Jenkins plugins, builds, pipelines, etc.
Java
107
star
88

jira-steps-plugin

Jenkins pipeline steps for integration with JIRA.
Java
104
star
89

artifactory-plugin

Jenkins artifactory plugin
Java
104
star
90

build-flow-plugin

A plugin to manage job orchestration
Groovy
103
star
91

throttle-concurrent-builds-plugin

Java
101
star
92

github-oauth-plugin

Jenkins authentication plugin using GitHub OAuth as the source.
Java
99
star
93

pipeline-graph-view-plugin

Java
99
star
94

promoted-builds-plugin

Jenkins Promoted Builds Plugin
Java
96
star
95

ssh-slaves-plugin

SSH Build Agents Plugin for Jenkins
Java
96
star
96

github-pr-coverage-status-plugin

Nice test coverage icon for your pull requests just from Jenkins
Java
93
star
97

jenkins-test-harness

Unit test framework for Jenkins core and its plugins
Java
92
star
98

opentelemetry-plugin

Monitor and observe Jenkins with OpenTelemetry.
Java
90
star
99

localization-zh-cn-plugin

Chinese Localization for Jenkins
HTML
89
star
100

workflow-remote-loader-plugin

Load Pipeline scripts from remote locations (e.g. Git) - replaced by Pipeline: Groovy Libraries plugin
Java
88
star