Calling the Ansible Automation Platform API Using 'Friendly'? Job Template Names

Calling the Ansible Automation Platform API Using 'Friendly' Job Template Names

Have you ever wanted to call the AAP/Tower API to launch a Job Template? The REST API allows you to do that, fast and efficiently. This means you can integrate AAP with orchestrator tools, like ServiceNow for instance.

AAP uses a Postgres database behind the scenes. When you create a Job Template, it gets a unique 'primary key' or id. You'll see this id in the Tower GUI, when you look at a Job Template:

No alt text provided for this image

You can see here that the id for my openshift_example Job Template is 68.

This is great. With the right access, we can make a call into the API and launch this job. But what if we have a number of platforms, and want to make sure our ids stay consistent when we create or recreate them? Well, you can't (and don't even *think* about trying to alter the database primary keys because you'll break things). But you can call the Job Template using Named URLs.

That documentation is a little hard to get your head around, hence this blog to give you an example usage to demonstrate :)

So we have a Job Template we'd like to launch through the AAP API in place - openshift_example. We need authorised access to make the call. You can use a username/password combo for this, but better to setup and use a Personal Access Token (PAT). So let's see how we do that now.

Creating A PAT

Go into the GUI Users section, select your user (in my case admin) and click Tokens:

No alt text provided for this image

Click the green + icon to create a new token:

Fill out the Description and make the scope Write:

No alt text provided for this image

When you click SAVE, you'll get presented with the PAT token. This is the only time you'll see this and you won't be able to edit it so make sure you save the details!

No alt text provided for this image

We'll use this PAT token now to make the remote API call.

Remote AAP Job Template Launch

I'm going to use curl from the command line to demonstrate this, so you'll get to see the format of the call, which is sometimes the hard part to figure out!

I'm also going to use the fantastic 'jq' utility to help parse the output coming back, so I can get the job id and then query for it's status.

Now at the moment I can launch Job Template id 68. But we want to use the friendly template name to keep things consistent, when, or if, the id changes. So we can use the named URL facility for this.

According to the docs... "Suppose you want to manually determine the named URL for a label with ID 5. A typical procedure of composing a named URL for this specific resource object using NAMED_URL_FORMATS is to first look up the labels field of NAMED_URL_FORMATS to get the identifier format <name>++<organization.name>"

Blimey. That could be a lot easier to understand! But this means we can use job_template_name++organsation_name in the API call, in place of the id.

So, to fire off a Job Template launch, we can use:

curl -s -k -X POST -H "Authorization: Bearer YOUR-PAT-TOKEN" -H "Content-Type: application/json" https://YOUR-TOWER-HOST/api/v2/job_templates/openshift_example++Default/launch/ | jq .job
2014

Let's break that down.

We need to make a -X POST HTTP request into the API.

I'm telling curl to be silent and ignore cert errors with -s -k

We pass YOUR-PAT-TOKEN as a Bearer token as a HTTP Header request.

Likewise, we make sure that the Content-type is as expected, application/json

The actual request is in the https://YOUR-TOWER-HOST section, ending with a /launch/

I then tell jq to get the job number, which is 2014.

If you look in AAP now, you'll see that in the Jobs output.

If you need to query the output, you then leave off the | jq part, and you'll get a whole load of information coming back.

If you want to just query the status of the job, we can make a HTTP GET request back into the API for that:

curl -s -k -X GET -H "Authorization: Bearer YOUR-PAT-TOKEN" -H "Content-Type: application/json" https://YOUR-TOWER-HOST/api/v2/jobs/2014/ | jq .status
"running"


You can see mine's running still. Keep making a few more calls to see the final status.

That's it. Quite simple but effective!

I hope that was useful for you. Happy automating!






Gujula archana

Apps system engineer at WellsFargo.com

1 年

how to pass extra variables which are needed for ansibleplaybook using curl,can you please suggest on that

回复

要查看或添加评论,请登录

Phil Griffiths的更多文章

  • Still using vi for Ansible? No problem!

    Still using vi for Ansible? No problem!

    Having grown up using vi as a main editor on UN*X systems, I still love it and often fire it up for quick demos etc. I…

    3 条评论
  • Creating a custom EE for AWX

    Creating a custom EE for AWX

    This is a quick and rough guide to creating and consuming a custom execution environment (EE) in AWX. What You'll Need…

    29 条评论
  • Ansible Execution Environments

    Ansible Execution Environments

    Introduction Ansible as a automation platform offering is evolving further, making customer automation runtimes easier…

    15 条评论
  • AWX 18.0.0 with Containerised Execution Environments

    AWX 18.0.0 with Containerised Execution Environments

    The upstream of Ansible Automation Platform, AWX has just landed with a new exciting release. Thought I'd give it a try…

    4 条评论
  • Ansible Features I Missed

    Ansible Features I Missed

    Well, ok some of these aren't particularly new, but I've only just become aware of them. Even after years of Ansible…

    4 条评论
  • Tuning RHEL Using Ansible System Roles

    Tuning RHEL Using Ansible System Roles

    Take the pain out of some performance tuning using RHEL's tuned functionality. Take even more pain out setting it up…

    1 条评论
  • Oh Molecule You've Come A Long Way

    Oh Molecule You've Come A Long Way

    I often get asked about Ansible testing, best practices and related topics. If there's one tool I'd have in the kit bag…

    4 条评论
  • Holy crap batman! sudo's bust!

    Holy crap batman! sudo's bust!

    As we all know, software has bugs, no one escapes! How you respond and fix those bugs is what's important. If you ever…

    1 条评论
  • RHEL OS Image Builder (part two)

    RHEL OS Image Builder (part two)

    This is part 2 of the blog series. Part one is here.

    1 条评论
  • RHEL8 OS Image Builder (part one)

    RHEL8 OS Image Builder (part one)

    Following on from this post for setting up a quick RHEL VM with minimal fuss, this is what I really wanted it for!…

社区洞察

其他会员也浏览了