本文介绍了Rundeck未设置环境变量以使用不同的ssh端口进行远程执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rundeck将传递给作业的所有选项设置为环境变量(如$RD_OPTION_*),但是当在具有不同ssh端口的远程节点中执行作业时,不会设置这些变量.该脚本成功登录到远程节点,但是环境变量不存在.请帮我解决.

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. The script logs into remote node successfully but environment variables are not there. Please help me with a solution.

示例工作定义:

<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='[email protected]' subject='job failure :(' />
      </onfailure>
      <onsuccess>
        <email recipients='[email protected]' 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.

-->

注意:

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

Rundeck is not setting up environment variables when executed on remote instance with different ssh port. In this case port is 2808 and the same has been updated in resources.xml as 123.456.789.0:2808. Rundeck logs into server and executes scripts successfully (without environment variables). Remote instance sshd_config has been configured to accept RD_* variables. The same Environment variables are set up and accessed when logged in using port 22.

推荐答案

我认为您混合了 Rundeck命令参数 Rundeck环境变量

这是命令,脚本参数和作业引用参数":${job.execid}

This is a "Commands, Script Arguments and Job Reference Arguments":${job.execid}

顾名思义,您可以将其用作命令参数.就像您在工作定义中所做的一样.

As its name states, you can use it as a commands arguments. Just like what you did in your job definition.

这是一个环境变量":$RD_JOB_EXECID

This is a "Environment Variables":$RD_JOB_EXECID

如果没有在Rundeck服务器本身上运行作业,则没有任何设置,两者都可以正常工作,但是如果要将作业分派到节点,则$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.

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

Use a wild card pattern to permit RD_ prefixed variables to provide open access to Rundeck generated environment variables.

 Example in sshd_config:

 # pass Rundeck variables 
 AcceptEnv RD_*

Rundeck SSH插件

在Rundeck服务器上

On Rundeck Server

确保在ssh_config中设置了SendEnv RD_*

Make sure you have SendEnv RD_* set in ssh_config

对于您的用例,${job.execid}${option.option1}可以完美地与sshd_config配合使用

For your usecase, ${job.execid},${option.option1} would works perfect with out mess around with 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>

这篇关于Rundeck未设置环境变量以使用不同的ssh端口进行远程执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 13:23