Skip to content

Add job options to ansible env#358

Open
len-ro wants to merge 11 commits into
rundeck-plugins:mainfrom
len-ro:main
Open

Add job options to ansible env#358
len-ro wants to merge 11 commits into
rundeck-plugins:mainfrom
len-ro:main

Conversation

@len-ro
Copy link
Copy Markdown

@len-ro len-ro commented Mar 31, 2024

This change adds all the "job" options to the process environment used to run ansible. It allows something like this to work:

    - debug:
        msg: 
          - "RD_JOB_NAME: {{lookup('ansible.builtin.env', 'RD_JOB_NAME')}}"
          - "RD_JOB_USERNAME: {{lookup('ansible.builtin.env', 'RD_JOB_USERNAME')}}"
          - "RD_JOB_URL: {{lookup('ansible.builtin.env', 'RD_JOB_URL')}}"

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.

@wwilkins-intlog
Copy link
Copy Markdown

@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());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#424 (comment)

looks like you need to look for a NullPointer here (it will fail in the GH actions tests as well)

@Lusitaniae
Copy link
Copy Markdown

Plain Text with Password Input

Tested this with the build on my fork and seems to work fine https://github.com/Lusitaniae/ansible-plugin

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 build RD_JOB_* environment variables from the job data 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.

Comment thread README.md
Comment on lines +1 to +2
> [!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.
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread README.md
@@ -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.
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo/grammar: “ability to proper identify” should be “ability to properly identify”.

Suggested change
> 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.

Copilot uses AI. Check for mistakes.
Comment on lines 120 to 124
Map<String,String> options = contextBuilder.getListOptions();
if (options != null) {
//also add all the job options
options.putAll(contextBuilder.getJobOptions());
ansibleRunnerBuilder.options(options);
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
@fdevans
Copy link
Copy Markdown
Contributor

fdevans commented Mar 24, 2026

@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!

len corciovei and others added 6 commits April 30, 2026 15:15
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants