Automation Scripts

Reply
Highlighted

python API NetMRI send variable to run Script broker->script->run

hoppem
Techie
Posts: 12
2398     0

Hello,

 

i used your python example to run a script.

 

Now i would send variable informations to the script.

 

But in the job there ist no values.

 

devices = broker.find(
    op_DeviceName='like',
    val_c_DeviceName=args.device_id,
    select=['DeviceID']
)
script_broker = client.get_broker('Script')
# submit the job to the NetMRI
job_id = script_broker.run(
        name=script_name,
        device_ids=[x.DeviceID for x in devices],
        script_variables = "$commands_to_be_executed 'show version'",
    )

"script_variables = "$commands_to_be_executed 'show version'" will not be used.

 

Can you send me a example with one and two variables ?

Re: python API NetMRI send variable to run Script broker->script->run

Adviser
Posts: 357
2399     0
You pass each variable as it’s own parameter. I’m not sure in Python, but in Perl that means that, ’$commands_to_be_executed’ should be key in the hash (dict) that gets passed in, just like ”device_ids” or ”name”.

Re: python API NetMRI send variable to run Script broker->script->run

hoppem
Techie
Posts: 12
2399     0

Hello JBelamaric,

 

thanks for your reply.

But in the API Dokumentation - Python - v3_2_0.ScriptBroker "broker.run - INPUTS" i found:

 

script_variables Required:False type: String
Optional variables to be passed to the script. Any variable name starting with $ will be passed through as input to the script.

But it will not work.

 

 

 

 

 

 

Re: python API NetMRI send variable to run Script broker->script->run

Adviser
Posts: 357
2399     0
Yes, the docs are not entirely clear there. I thought there was more description about this in there, that you shouldn’t create an actual input called ’script_variables’ - instead you should create inputs named for each of your variables, including the $ in front.

Re: python API NetMRI send variable to run Script broker->script->run

hoppem
Techie
Posts: 12
2399     0

Hello JBelamari,

 

what ist wrong on this line  ? 

 

script_variables = "$commands_to_be_executed 'show version'",

 

Complete Lines:

job_id = script_broker.run(
        name=script_name,
        device_ids=[x.DeviceID for x in devices],
        script_variables = "$commands_to_be_executed 'show version'",
    )

The JOB starts but with no commdand 'show version'.

What is wrong ?

 

Re: python API NetMRI send variable to run Script broker->script->run

Adviser
Posts: 357
2399     0
You shouldn’t have a parameter named script_variables at all. Instead, you should have ’$commands_to_execute’ as *it’s own parameter*. Every parameter passed in that starts with ’$’ will be treated as a command input. The documentation here is not clear, we need to fix that.

Re: python API NetMRI send variable to run Script broker->script->run

Adviser
Posts: 357
2399     0
Not sure if it works exactly like this in Python, but it would be something like:


job_id = script_broker.run(
name=script_name,
device_ids=[x.DeviceID for x in devices],
”$commands_to_be_executed”="show version",
)

Though it might need to be a bit different syntax in Python.

Re: python API NetMRI send variable to run Script broker->script->run

hoppem
Techie
Posts: 12
2399     0

Your Example did not work:

 

job_id = script_broker.run(
name=script_name,
device_ids=[x.DeviceID for x in devices],
"$commands_to_be_executed"="show version",
)

I get this error message: "SyntaxError: keyword can't be an expression"

Re: python API NetMRI send variable to run Script broker->script->run

Expert
Posts: 139
2399     0

Here is python script function from Sif that I adapted/adopted, it uses the api instead of the broker.  The device id is hard coded:

 

def run_adhoc():
        data = client.api_request(
                'scripts/run', params={
                        'name': "Ad Hoc Command Batch",
                        '$commands_to_be_executed': "sh version",
                        'device_ids': "290",
                        'credential_mode':'requestor',
                }
        )

-Lon

 

Re: python API NetMRI send variable to run Script broker->script->run

Adviser
Posts: 357
2399     0
I have asked the team to look into this in Python and how it can be done more cleanly/documented better.

The last example in the Perl Tutorial in the documentation shows how to do it in Perl:


# run the script against the devices specified on the
# command line.
my($job_id) = $client->broker->script->run({
name => 'API Example 9 - Synchronous Perl Job',
device_ids => \@ARGV,
'$command' => 'show version',
})->{JobID};

# fetch the Job object
my $job = $client->broker->job->show({ JobID => $job_id })->{job};


John

Re: python API NetMRI send variable to run Script broker->script->run

hoppem
Techie
Posts: 12
2399     0

I have the solution Smiley Happy

This tip helps me: Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

 

defaults = {
    "host": hostname,
    "username": username,
    "password": password,
}

client = InfobloxNetMRI(
    defaults.get("host"),
    defaults.get("username"),
    defaults.get("password"),
#    api_version=2.8,
)

# get broker for specific API Object
broker = client.get_broker('Device')

# get devices
job_commmands = "show version"
devices = broker.find(
    op_DeviceName='like',
    val_c_DeviceName=args.device_id,
    select=['DeviceID']
)
script_broker = client.get_broker('Script')
save_new = {
    "name" : script_name,
    "device_ids" : [x.DeviceID for x in devices],
    "$commands_to_be_executed" : job_commmands,
    "job_name" : "Show config",
    }

job_id = script_broker.run(**save_new)

 

Thies Person inspired me (thanks to): JBelamaric, SBaksh, LThompson

 

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You