簡體   English   中英

Rundeck未設置環境變量以使用其他ssh端口進行遠程執行

[英]Rundeck not setting up environment variable for remote execution with different ssh port

Rundeck將所有傳遞給作業的選項設置為$RD_OPTION_*等環境變量,但在具有不同ssh端口的遠程節點中執行作業時,則不會設置這些變量。 該腳本成功登錄到遠程節點,但是環境變量不存在。 請幫我解決。

作業定義示例:

 <joblist> <job> <context> <options preserveOrder='true'> <option name='option1' required='true' /> </options> </context> <description>job description</description> <dispatch> <excludePrecedence>true</excludePrecedence> <keepgoing>false</keepgoing> <rankOrder>ascending</rankOrder> <threadcount>1</threadcount> </dispatch> <executionEnabled>true</executionEnabled> <id>id</id> <loglevel>DEBUG</loglevel> <name>job name</name> <nodefilters> <filter>name: remote_node</filter> </nodefilters> <nodesSelectedByDefault>true</nodesSelectedByDefault> <notification> <onfailure> <email attachLog='true' recipients='abcdef@xyz.com' subject='job failure :(' /> </onfailure> <onsuccess> <email recipients='abcdef@xyz.com' subject='job succes' /> </onsuccess> </notification> <scheduleEnabled>true</scheduleEnabled> <sequence keepgoing='false' strategy='step-first'> <command> <exec>python path/to/script.py $RD_OPTION_OPTION1 > /path/to/logfile_$RD_JOB_EXECID.log 2>&1</exec> </command> <command> <exec>java -jar path/to/jarfile.jar ${option.option1} >> "/path/to/logfile_${job.execid}.log" 2>&1</exec> </command> </sequence> <uuid>job-uuid</uuid> </job> </joblist> <!-- Here $RD_JOB_EXECID,${job.execid},${option.option1},$RD_OPTION_OPTION1 are not being setup as environment variables when remote node is selected for execution but the same variables are set up as environment variables when executed locally. Rundeck logins to the remote node as user successfully. Log entries are seen in /path/to/logfile_.log file in remote node since $RD_JOB_EXECID has not been set up. the options @option.option1@ are working fine since they have been replaced by rundeck before executing command. Rundeck details: user: rundeck shell: /bin/nologin rundeck logs into remote server as normal user who has all permissions to execute all these scripts/jars. --> 

注意:

在具有不同ssh端口的遠程實例上執行時,Rundeck不會設置環境變量。 在這種情況下,端口為2808,並且resources.xml中的端口已更新為123.456.789.0:2808 Rundeck登錄到服務器並成功執行腳本(沒有環境變量)。 遠程實例sshd_config已配置為接受RD_ *變量。 使用端口22登錄時,將設置和訪問相同的環境變量。

我認為您混淆了Rundeck命令參數Rundeck環境變量

這是“命令,腳本參數和作業引用參數”: ${job.execid}

顧名思義,您可以將其用作命令參數。 就像您在工作定義中所做的一樣。

這是一個“環境變量”: $RD_JOB_EXECID

沒有任何設置,如果您在Rundeck服務器本身上運行作業,兩者都可以正常工作,但是如果您要將作業分派到節點,則$RD_JOB_EXECID將無法立即使用。

要通過遠程命令分派傳遞環境變量,需要在遠程端正確配置SSH服務器。 有關說明,請參見“ sshd_config(5)” 手冊頁中的AcceptEnv指令。

使用通配符模式允許RD_前綴變量提供對Rundeck生成的環境變量的開放訪問。

 Example in sshd_config:

 # pass Rundeck variables 
 AcceptEnv RD_*

Rundeck SSH插件

在Rundeck服務器上

確保在ssh_config中設置了SendEnv RD_*

對於您的用例, ${job.execid}${option.option1}可以完美地與sshd_config配合使用


它可以在不同的SSH端口上工作。

在此處輸入圖片說明

XML中的作業定義

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='nodeFilter' />
      </options>
    </context>
    <description></description>
    <dispatch>
      <excludePrecedence>true</excludePrecedence>
      <keepgoing>false</keepgoing>
      <rankOrder>ascending</rankOrder>
      <threadcount>1</threadcount>
    </dispatch>
    <executionEnabled>true</executionEnabled>
    <group>TEST</group>
    <id>63b6f283-39b2-479d-bba9-b1742bc2ea53</id>
    <loglevel>INFO</loglevel>
    <name>test rundeck job context</name>
    <nodefilters>
      <filter>${option.nodeFilter}</filter>
    </nodefilters>
    <nodesSelectedByDefault>true</nodesSelectedByDefault>
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <script><![CDATA[#!/usr/bin/python
import sys
print "I know ENV_VAR will not work as command line arguments %s " % sys.argv
]]></script>
        <scriptargs> "&gt;${job.execid}&lt; &gt;$RD_JOB_EXECID&lt;"</scriptargs>
      </command>
      <command>
        <script><![CDATA[#!/bin/bash
echo "But it works in Bash"
echo $RD_JOB_ID
echo $RD_JOB_EXECID

echo "Which port does sshd listening on?"
sudo netstat -tulpn | grep 2808]]></script>
        <scriptargs />
      </command>
    </sequence>
    <uuid>63b6f283-39b2-479d-bba9-b1742bc2ea53</uuid>
  </job>
</joblist>

正如Yang所說,這是由於ssh配置所致。 同樣AcceptEnv變量sshd_configSendEnv變量ssh_config ,所以我必須指定RD_*SendEnv這樣SendEnv RD_*本地主機ssh_config中 ,這將指導SSH這些環境變量發送到服務器。 我發現使用環境變量時需要完成以下工作

  1. 需要在localhost的 ssh_config文件中設置SendEnv來發送環境變量。
  2. 為了接受傳遞到服務器的環境變量,需要在遠程節點的 sshd_config文件中設置AcceptEnv

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM