Skip to content

Commit

Permalink
use name or id for inventory or credential
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Moussaud committed Sep 18, 2018
1 parent f69d0fa commit 15aa199
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/main/jython/ansible_tower/run_job_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@
from ansible_tower.connect_util import session


def get_resource_id(resource, name_or_id):
if name_or_id.isdigit():
return int(name_or_id)
result = resource.list(name=name_or_id)
count = int(result['count'])
if count == 0:
raise Exception("Resource name '%s''%s' not found " % (resource, name_or_id))
if count > 1:
raise Exception("Too many result for resource name '%s''%s' not found " % (resource, name_or_id))
return int(result['results'][0]['id'])


def process(task_vars):
with session(task_vars['tower_server'], task_vars['username'], task_vars['password']):
job = get_resource('job')

try:
k_vars = {}
if task_vars['inventory']:
print("* set inventory : {0}".format(task_vars['inventory']))
k_vars['inventory'] = task_vars['inventory']
result = get_resource_id(get_resource('inventory'), task_vars['inventory'])
print("* set inventory : {0}->{1}".format(task_vars['inventory'], result))
k_vars['inventory'] = result

if task_vars['credential']:
print("* set credentials : {0}".format(task_vars['credential']))
k_vars['credential'] = task_vars['credential']
result = get_resource_id(get_resource('credential'), task_vars['credential'])
print("* set credentials : {0}->{1}".format(task_vars['credential'], result))
k_vars['credential'] = result

if task_vars['extraVars']:
print("* set extra_vars : {0}".format(task_vars['extraVars']))
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/synthetic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
<!-- input params -->
<property name="jobTemplate" category="input" required="true"
description="Primary key or name of the job template to launch new job."/>
<property name="inventory" category="input" kind="integer" required="false"
description="A integer value for the foreign key of an inventory to use in this job run"/>
<property name="credential" category="input" kind="integer" required="false"
description="A integer value for the foreign key of a credential to use in this job run"/>
<property name="inventory" category="input" kind="string" required="false"
description="The inventory name of a integer value for the foreign key of an inventory to use in this job run"/>
<property name="credential" category="input" kind="string" required="false"
description="The credential name or a integer value for the foreign key of a credential to use in this job run"/>
<property name="extraVars" category="input" required="false" kind="list_of_string" size="large"
description="yaml formatted texts that contains extra variables to pass on."/>
<property name="taskPasswordToken" required="false" category="input"/>
Expand Down

0 comments on commit 15aa199

Please sign in to comment.