Search code examples
pythonsubprocesspython-3.7rundeckproxmox

Python script that get HW info about RAM working inside host but fails as a Rundeck Job


I have a custom python script that collects information (Capacity, Vendors, Manufacturer, etc) about the hardware (CPU, RAM, etc) of a server host, and dumps that information in json formatted output files (i.e. memory_info.json - later to be used by another script for something else). The script makes use of python subprocess in order to call external programs like "inxi" in this case.

After some testing in various hosts (different OS, archs, etc) it seems to work fine, so I decided to configure it, in order to run as a Rundeck job (for central management purposes). The rundeck job is very simple and it just executes the script in the following way:

sshpass -p ${RD_OPTION_SERVERPASSWORD} ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q -t root@${RD_OPTION_SERVERIPADDRESS} "python3 /tmp/hw_inxi.py"

Here comes the problem:

Executing it as a rundeck job, i get the following traceback:

Traceback (most recent call last):
  File "/tmp/hw_inxi.py", line 53, in <module>
    "--output-file", "print", "--output", "json"]).strip().decode()
  File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.7/subprocess.py", line 487, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['inxi', '-xxxm', '--output-file', 'print', '--output', 'json']' returned non-zero exit status 1.
Result: 1
Failed: NonZeroResultCode: Result code was 1
Execution failed: 2541 in project server_setup: [Workflow result: , step failures: {1=Dispatch failed on 1 nodes: [RUNDECK_SERVER: NonZeroResultCode: Result code was 1 + {dataContext=MultiDataContextImpl(map={ContextView(node:RUNDECK_SERVER)=BaseDataContext{{exec={exitCode=0}}}, ContextView(step:1, node:RUNDECK_SERVER)=BaseDataContext{{exec={exitCode=0}}}}, base=null)} ]}, Node failures: {RUNDECK_SERVER=[NonZeroResultCode: Result code was 1 + {dataContext=MultiDataContextImpl(map={ContextView(node:RUNDECK_SERVER)=BaseDataContext{{exec={exitCode=0}}}, ContextView(step:1, node:RUNDECK_SERVER)=BaseDataContext{{exec={exitCode=0}}}}, base=null)} ]}, status: failed]

Inxi is installed and properly configured in the destination machine and binary permissions seem ok. My script run perfectly fine with no error from within the server and the problem appears when it executes as a rundeck job (Rundeck version 3.3.10).

The relative code snippet from the script can be seen below:

inxi_mem = subprocess.check_output(["inxi", "-xxxm", "--output-file", "print", "--output", "json"]).strip().decode()
memory_list = json.loads(inxi_mem)

Any ideas on who may be the culprit or how to troubleshoot the above?

(p.s. sorry if I forgot something, this is my first question)


Solution

  • I am using a job step to connect to the remote host, on purpose, as this is the only way to use the 2nd script with the hardware data that the 1st script gathered, from within my Rundeck server.

    The problem seems to be related with environment variables and it was solved by adding "--tty" option in the inxi command as mentioned in this issue. The subprocess command now looks like below and works like a charm.

    inxi_mem = subprocess.check_output(["inxi", "--tty", "-xxxm", "--output-file", "print", "--output", "json"]).strip().decode()
    memory_list = json.loads(inxi_mem)