<p>You can use -e switch for that purpose:</p>

<pre><code>ansible-playbook -e 'foo=${FOO}'
</code></pre>

<p>where the FOO can be defined in the Jenkins envirionment variables. If it is defined within the parameters block of your Jenkinsfile, then you need to use <code>${params.foo}</code>. Then, the value can be accessed by the Ansible playbook with <code>{{ foo }}</code>.</p>

CC BY-SA 4.0
2019-12-16T07:56:49.673Z
2019-12-16T08:11:02.677Z
2019-12-16T08:11:02.677Z
kporwit
<p>If you're using Jenkins <a href="https://plugins.jenkins.io/ansible/" rel="nofollow noreferrer">Ansible plugin</a>, Jenkins pipeline envvars (and build vars) are injected automatically into the playbooks, where they can be accessed like so:</p>
<pre><code>---
- hosts: example
  tasks:
    - debug: msg=&quot;{{ lookup('env','MY_PIPELINE_ENVVAR') }}&quot;
</code></pre>
<p>If you want to transit pipeline params to Ansible, you can define an environment block in your Jenkins pipeline where you convert the params to envvars simply:</p>
<pre><code>pipeline {
  environment {
    MY_PIPELINE_ENVVAR = &quot;${params.MY_PIPELINE_PARAM}&quot;
  }
  ...
}
</code></pre>
<p>If you want all of your params automatically converted to envvars that are then automatically all injected via ansiblePlaybook to the playbooks, you can do something like this:</p>
<pre><code>environment {
  script {
    params.each { key, value -&gt;
      env.&quot;$key&quot; = value
    }
  }
}
</code></pre>
<p>This way, if you have a params named, for example, <code>VAR1</code>, <code>VAR2</code>, and <code>VAR3</code>, they become envvars by the same names, and then subsequently can be accessed in the playbooks using the above-mentioned <code>lookup</code>.</p>

CC BY-SA 4.0
2024-04-17T02:54:09.117Z
2024-04-17T03:03:20.243Z
2024-04-17T03:03:20.243Z
Ville
<p>How can I pass parameters from Jenkins to my Ansible playbook? Eg. I want to take IP address from users and pass it to the playbook so that it will execute only on those machines.</p>

<jenkins><ansible>
Passing parameters from Jenkins to Ansible playbook
Harshdeep Koul
# Passing parameters from Jenkins to Ansible playbook

(Asked by: username_0 on 2019-12-14)

How can I pass parameters from Jenkins to my Ansible playbook? Eg. I want to take IP address from users and pass it to the playbook so that it will execute only on those machines.

**Tags:** jenkins, ansible


---

## 2 answers

### Answer by username_1 on 2019-12-16. Score: 2

You can use -e switch for that purpose:
ansible-playbook -e 'foo=${FOO}'

where the FOO can be defined in the Jenkins envirionment variables. If it is defined within the parameters block of your Jenkinsfile, then you need to use ${params.foo}. Then, the value can be accessed by the Ansible playbook with {{ foo }}.


---

### Answer by username_2 on 2024-04-17. Score: 0

If you're using Jenkins Ansible plugin, Jenkins pipeline envvars (and build vars) are injected automatically into the playbooks, where they can be accessed like so:
---
- hosts: example
  tasks:
    - debug: msg="{{ lookup('env','MY_PIPELINE_ENVVAR') }}"

If you want to transit pipeline params to Ansible, you can define an environment block in your Jenkins pipeline where you convert the params to envvars simply:
pipeline {
  environment {
    MY_PIPELINE_ENVVAR = "${params.MY_PIPELINE_PARAM}"
  }
  ...
}

If you want all of your params automatically converted to envvars that are then automatically all injected via ansiblePlaybook to the playbooks, you can do something like this:
environment {
  script {
    params.each { key, value ->
      env."$key" = value
    }
  }
}

This way, if you have a params named, for example, VAR1, VAR2, and VAR3, they become envvars by the same names, and then subsequently can be accessed in the playbooks using the above-mentioned lookup.


---

