如何使用Rundeck执行带多值参数的远程命令?
Great question! Let’s break down the best options for your scenario, depending on how you plan to use this workflow.
1. 内联脚本(适合临时/adhoc操作)
If you just need a quick, one-off way to run these scripts on a remote node, inline scripts over SSH work perfectly. The key is to handle the multi-value list safely to avoid variable expansion issues.
For example, if you’ve already captured the selected folders in a local array:
# Local variable holding your selected folders (e.g., from user input or parameters) SELECTED_FOLDERS=("folder1" "folder3") # Run inline script via SSH to iterate through each folder ssh your-user@remote-host ' for folder in "$@"; do echo "Executing script in /home/$folder..." /home/$folder/run.sh done ' -- "${SELECTED_FOLDERS[@]}"
Using "$@" to pass parameters ensures safe handling of folder names (even if they had special characters, though your use case uses fixed names so this is extra safe).
2. 配置管理工具(适合自动化/可维护场景)
If this is part of a repeatable automation workflow, tools like Ansible make this cleaner and add built-in parameter validation.
Take Ansible as an example: you can define a multi-value variable, loop through it, and restrict execution to only your allowed folders:
- name: Run scripts in specified folders hosts: remote-nodes vars: # Pass this via command line like: ansible-playbook -e "selected_folders=['folder1','folder2']" selected_folders: [] tasks: - name: Execute run.sh for valid folders shell: "/home/{{ item }}/run.sh" loop: "{{ selected_folders }}" # Ensure we only run scripts for allowed folder values when: item in ['folder1', 'folder2', 'folder3']
This approach makes parameter validation explicit, improves log readability, and scales easily to multiple remote nodes.
3. 封装远程脚本(适合频繁重复操作)
If you’ll be running this task often, pre-deploying a small wrapper script on the remote node simplifies local calls and centralizes logic:
First, create /home/your-user/run_selected_folders.sh on the remote node:
#!/bin/bash ALLOWED_FOLDERS=("folder1" "folder2" "folder3") for folder in "$@"; do if [[ " ${ALLOWED_FOLDERS[@]} " =~ " $folder " ]]; then /home/$folder/run.sh else echo "Error: $folder is not an allowed folder" >&2 fi done
Make it executable: chmod +x /home/your-user/run_selected_folders.sh
Then call it from your local machine with just:
ssh your-user@remote-host "/home/your-user/run_selected_folders.sh folder1 folder2"
This keeps local commands short and lets you update the validation logic in one place on the remote node.
Quick Recap
- Temporary, one-off tasks: Inline scripts via SSH are the fastest and most direct choice.
- Automated workflows: Use a configuration management tool’s loop functionality for maintainability and validation.
- Frequent repeat tasks: A pre-built remote wrapper script simplifies calls and centralizes logic.
内容的提问来源于stack exchange,提问作者Aleksey Shutov

