简体   繁体   English

Rundeck未设置环境变量以使用其他ssh端口进行远程执行

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

Rundeck setsup all the options passed to a job as environment variable like $RD_OPTION_* but it is not setting up those variables when a job is executed in remote node with different ssh port. Rundeck将所有传递给作业的选项设置为$RD_OPTION_*等环境变量,但在具有不同ssh端口的远程节点中执行作业时,则不会设置这些变量。 The script logs into remote node successfully but environment variables are not there. 该脚本成功登录到远程节点,但是环境变量不存在。 Please help me with a solution. 请帮我解决。

Sample Job definition: 作业定义示例:

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

Note: 注意:

Rundeck is not setting up environment variables when executed on remote instance with different ssh port. 在具有不同ssh端口的远程实例上执行时,Rundeck不会设置环境变量。 In this case port is 2808 and the same has been updated in resources.xml as 123.456.789.0:2808 . 在这种情况下,端口为2808,并且resources.xml中的端口已更新为123.456.789.0:2808 Rundeck logs into server and executes scripts successfully (without environment variables). Rundeck登录到服务器并成功执行脚本(没有环境变量)。 Remote instance sshd_config has been configured to accept RD_* variables. 远程实例sshd_config已配置为接受RD_ *变量。 The same Environment variables are set up and accessed when logged in using port 22 . 使用端口22登录时,将设置和访问相同的环境变量。

I think you mixed up Rundeck commands arguments and Rundeck environment variables 我认为您混淆了Rundeck命令参数Rundeck环境变量

This is a "Commands, Script Arguments and Job Reference Arguments": ${job.execid} 这是“命令,脚本参数和作业引用参数”: ${job.execid}

As its name states, you can use it as a commands arguments. 顾名思义,您可以将其用作命令参数。 Just like what you did in your job definition. 就像您在工作定义中所做的一样。

This is a "Environment Variables": $RD_JOB_EXECID 这是一个“环境变量”: $RD_JOB_EXECID

Without any setup, both work fine if you are running the job on Rundeck server itself, but if you want to dispatch your job to a node, $RD_JOB_EXECID will not work out of box. 没有任何设置,如果您在Rundeck服务器本身上运行作业,两者都可以正常工作,但是如果您要将作业分派到节点,则$RD_JOB_EXECID将无法立即使用。

To pass environment variables through remote command dispatches, it is required to properly configure the SSH server on the remote end. 要通过远程命令分派传递环境变量,需要在远程端正确配置SSH服务器。 See the AcceptEnv directive in the "sshd_config(5)" manual page for instructions. 有关说明,请参见“ sshd_config(5)” 手册页中的AcceptEnv指令。

Use a wild card pattern to permit RD_ prefixed variables to provide open access to Rundeck generated environment variables. 使用通配符模式允许RD_前缀变量提供对Rundeck生成的环境变量的开放访问。

 Example in sshd_config:

 # pass Rundeck variables 
 AcceptEnv RD_*

Rundeck SSH Plugins Rundeck SSH插件

On Rundeck Server 在Rundeck服务器上

Make sure you have SendEnv RD_* set in ssh_config 确保在ssh_config中设置了SendEnv RD_*

For your usecase, ${job.execid} , ${option.option1} would works perfect with out mess around with sshd_config 对于您的用例, ${job.execid}${option.option1}可以完美地与sshd_config配合使用


It DOES work on differect SSH port. 它可以在不同的SSH端口上工作。

在此处输入图片说明

Job Definition in XML 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>

As Yang said it was due to ssh configurations. 正如Yang所说,这是由于ssh配置所致。 Likewise AcceptEnv variable in sshd_config there is SendEnv variable in ssh_config so I have to specify RD_* in SendEnv like this SendEnv RD_* in localhost ssh_config which will instruct ssh to send those environment variables to server. 同样AcceptEnv变量sshd_configSendEnv变量ssh_config ,所以我必须指定RD_*SendEnv这样SendEnv RD_*本地主机ssh_config中 ,这将指导SSH这些环境变量发送到服务器。 I found out the following things that needs to be done when working with environment variables 我发现使用环境变量时需要完成以下工作

  1. SendEnv needs to be setup in ssh_config file in localhost for sending the environment variables. 需要在localhost的 ssh_config文件中设置SendEnv来发送环境变量。
  2. AcceptEnv needs to be setup in sshd_config file in remote node in order to accept environment variables passed to the server. 为了接受传递到服务器的环境变量,需要在远程节点的 sshd_config文件中设置AcceptEnv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM