Add job options to ansible env#358
Conversation
|
@len-ro Does this work with "Plain Text with Password Input" job inputs as well? |
| Map<String, String> jobOptions = context.getDataContext().get("job"); | ||
| for (Map.Entry<String, String> entry : jobOptions.entrySet()) { | ||
| if(entry.getValue() != null) { | ||
| options.put("RD_JOB_" + entry.getKey().toUpperCase(), entry.getValue()); |
There was a problem hiding this comment.
looks like you need to look for a NullPointer here (it will fail in the GH actions tests as well)
Tested this with the build on my fork and seems to work fine https://github.com/Lusitaniae/ansible-plugin |
There was a problem hiding this comment.
Pull request overview
Adds Rundeck “job” data context values to the environment of the Ansible process so playbooks/callbacks can read variables like RD_JOB_USERNAME, aligning behavior more closely with Rundeck script steps.
Changes:
- Add
getJobOptions()to buildRD_JOB_*environment variables from thejobdata context. - Merge job-derived env vars into the existing env var map passed to the Ansible process.
- Document the intent at the top of the README.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerContextBuilder.java | Adds helper to convert job data context entries into RD_JOB_* env vars |
| src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunner.java | Injects job-derived env vars into the runner’s environment options |
| README.md | Adds a tip describing the environment variable forwarding behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| > [!TIP] | ||
| > This fork main purpose is to forward the RUNDECK variables (such as RD_JOB_USERNAME, etc.) to the Ansible playbook. One reason is to give the ansible playbook the ability to proper identify who is running the playbook. |
There was a problem hiding this comment.
The README addition frames this change as “This fork main purpose…”, but this repository’s README reads like the upstream Rundeck Ansible plugin (links/issues point to rundeck-plugins/ansible-plugin). This wording is likely inaccurate/misleading for users and doesn’t match the PR description (which is about forwarding job context env vars). Consider rewriting this note to describe the feature generically (without referencing a fork) and placing it in an appropriate section.
| @@ -1,3 +1,8 @@ | |||
| > [!TIP] | |||
| > This fork main purpose is to forward the RUNDECK variables (such as RD_JOB_USERNAME, etc.) to the Ansible playbook. One reason is to give the ansible playbook the ability to proper identify who is running the playbook. | |||
There was a problem hiding this comment.
Typo/grammar: “ability to proper identify” should be “ability to properly identify”.
| > This fork main purpose is to forward the RUNDECK variables (such as RD_JOB_USERNAME, etc.) to the Ansible playbook. One reason is to give the ansible playbook the ability to proper identify who is running the playbook. | |
| > This fork main purpose is to forward the RUNDECK variables (such as RD_JOB_USERNAME, etc.) to the Ansible playbook. One reason is to give the ansible playbook the ability to properly identify who is running the playbook. |
| Map<String,String> options = contextBuilder.getListOptions(); | ||
| if (options != null) { | ||
| //also add all the job options | ||
| options.putAll(contextBuilder.getJobOptions()); | ||
| ansibleRunnerBuilder.options(options); |
There was a problem hiding this comment.
New behavior adds job context variables into the Ansible process environment, but there’s no test coverage validating (a) expected RD_JOB_* keys are added and (b) execution doesn’t fail when the job data context is absent. Consider adding/adjusting a Spock spec around buildAnsibleRunner / env var construction to cover these cases.
| Map<String,String> options = contextBuilder.getListOptions(); | |
| if (options != null) { | |
| //also add all the job options | |
| options.putAll(contextBuilder.getJobOptions()); | |
| ansibleRunnerBuilder.options(options); | |
| Map<String, String> listOptions = contextBuilder.getListOptions(); | |
| Map<String, String> jobOptions = contextBuilder.getJobOptions(); | |
| if (listOptions != null || jobOptions != null) { | |
| Map<String, String> mergedOptions = new HashMap<String, String>(); | |
| if (listOptions != null) { | |
| mergedOptions.putAll(listOptions); | |
| } | |
| if (jobOptions != null) { | |
| mergedOptions.putAll(jobOptions); | |
| } | |
| ansibleRunnerBuilder.options(mergedOptions); |
|
@len-ro - If you are able to handle the merge conflicts and address CoPilot feedback we can consider moving this PR forward in the process. Thank you for the contribution! |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This change adds all the "job" options to the process environment used to run ansible. It allows something like this to work:
In my case this allows me to use the RD_JOB_USERNAME in a custom ansible callback to identify the user calling the job.
Before this change these variables were not present in ansible unlike in a shell script ran from rundeck.
Thanks.