Rundeck没有为不同的ssh端口设置环境变量进行远程执行

时间:2017-04-10 06:03:47

标签: automation rundeck

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.

-->
&#13;
&#13;
&#13;

注意:

在具有不同ssh端口的远程实例上执行时,Rundeck没有设置环境变量。在这种情况下,端口是2808,并且在resources.xml中已将其更新为123.456.789.0:2808。 Rundeck登录到服务器并成功执行脚本(没有环境变量)。远程实例sshd_config已配置为接受RD_ *变量。使用端口22登录时,可以设置和访问相同的环境变量。

2 个答案:

答案 0 :(得分:4)

我认为你混淆了 Rundeck命令参数 Rundeck环境变量

这是一个&#34;命令,脚本参数和作业参考参数&#34;: ${job.execid}

正如其名称所述,您可以将其用作命令参数。就像你在工作定义中所做的那样。

这是一个&#34;环境变量&#34;: $RD_JOB_EXECID

如果您在Rundeck服务器本身上运行作业,则无需任何设置,两者都可以正常工作,但如果您要将作业分配到某个节点,$RD_JOB_EXECID将无法开箱即用。

  

要通过远程命令调度传递环境变量,它是   需要在远程端正确配置SSH服务器。 查看   &#34; sshd_config(5)&#34;中的AcceptEnv指令manual page   指令。

     

使用通配符模式允许提供RD_前缀变量   对Rundeck生成的环境变量的开放访问。

 Example in sshd_config:

 # pass Rundeck variables 
 AcceptEnv RD_*

Rundeck SSH Plugins

在Rundeck Server上

确保在ssh_config中设置了SendEnv RD_*

对于您的用例,${job.execid}${option.option1}可以完美地解决sshd_config问题

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

enter image description here

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>

答案 1 :(得分:1)

杨说这是由于ssh配置。同样AcceptEnv中的sshd_config变量SendEnv中有ssh_config变量,因此我必须在RD_*中指定SendEnv,如此SendEnv RD_* localhost ssh_config 中,它将指示ssh将这些环境变量发送到服务器。我在使用环境变量时发现了以下需要做的事情

  1. SendEnv需要在 localhost ssh_config文件中设置,以便发送环境变量。
  2. AcceptEnv需要在远程节点sshd_config文件中设置,以接受传递给服务器的环境变量。