Science and technology

Using Ansible with REST APIs

Ansible is a prime open supply undertaking which, on the floor, appears to be like to supply a easy method to standardize your present automation and permit it to run in parallel throughout a number of hosts, and it does this very efficiently. Yet, in actuality, Ansible has the capabilities to increase what your present automation does to include different programs and actually simplify duties throughout all features of your each day routine.

This functionality begins with the collections and roles which can be included with Ansible and all of the third-party utilities distributed by Ansible Galaxy. You could have queried APIs with an internet browser or curl, however one of many missed capabilities of Ansible is how effectively it will probably leverage APIs as a part of any playbook. This is extraordinarily helpful as a result of the variety of REST APIs being constructed and deployed each internally and throughout the worldwide web is growing exponentially. There’s even a public-apis GitHub repo itemizing a whole bunch of free APIs throughout over a dozen classes only for a way of scale.

A primary API playbook

Well, it actually comes down to some key core capabilities inside Ansible, that are uncovered properly with one particular built-in job, uri. In this publish, I will undergo a reasonably easy instance of the way to name a REST API and use the info from that decision to determine what to do subsequent. This works with Ansible 2.9 and better. In later variations (particularly v4), the modules we use have to be prepended with ansible.builtin like ansible.builtin.set_fact as an alternative of simply set_fact.

To get began, you want a primary playbook to construct on. In this case, you are solely utilizing native calls, so you do not have to be a superuser.

First, create this YAML file to determine a working baseline:

---
- title
: Using a REST API
  grow to be
: false
  hosts
: localhost
  gather_facts
: false
  duties
:
    - debug
:
        msg
: “Let’s call an API”

Here’s the output after operating it:

% ansible-playbook using-a-rest-api.yml

PLAY [Using a REST API] *********************************************************************************************

TASK [debug] ********************************************************************************************************
okay: [localhost] =>
    "msg": "“Let’s call an API”"

PLAY RECAP **********************************************************************************************************
localhost                  : okay=1    modified=zero    unreachable=zero    failed=zero    skipped=zero    rescued=zero    ignored=zero  

Calling an API

To name an precise API, you should utilize the uri module. Here are two examples. The first is only a GET and the second is a POST with parameters to indicate the completely different accessible choices.

---
- title
: Everyone loves a very good Chuck Norris joke
  uri
:
    url
: https://api.chucknorris.io/jokes/random
    technique
: GET

- title
: Login to an API
  uri
:
    url
: https://auth.example.com/oauth/access_token
    technique
: POST
    body_format
: json
    physique
:
      title
: your_username
      password
: your_password
      client_id
: YOUR_CLIENT_ID
      access_token
: ACCESS_TOKEN
      connection
: CONNECTION
      scope
: SCOPE

I take advantage of the primary API for the remainder of this text to indicate how the returned information can be utilized. The query is, how do you gather the info being returned, and what does it appear to be?

To gather the output from any job operating in Ansible, you utilize the register attribute, after which you should utilize the debug job to show the uncooked information. In the case of APIs referred to as utilizing uri, all of the output is put beneath the .json. Subsection of the consequence. The uri instructions and different its output are additionally at that prime stage. These could be helpful to ensure the API name works by taking a look at different information fields like standing.

These are the 2 duties you could add to the unique playbook so as to add the API name to the combination to later do one thing with.

  - title: Getting the definition of superior
      uri
:
        url
: https://api.chucknorris.io/jokes/random
        technique
: GET
      register
: outcomes

    - debug
:
        var
: outcomes

Run it to see the output generated by debug:

TASK [debug] ********************************************************************************************************
okay: [localhost] =>

Now which you can see all of the output make a customized message itemizing the worth returned by the API. Here is the finished playbook:

---
- title
: Using a REST API
  grow to be
: false
  hosts
: localhost
  gather_facts
: false
  duties
:
    - debug
:
        msg
: “Let’s call an API”

    - title
: Everyone loves a very good Chuck Norris joke
      uri
:
        url
: https://api.chucknorris.io/jokes/random
        technique
: GET
      register
: outcomes

    - debug
:
        var
: outcomes.json.worth

And now the entire output:

PLAY [Using a REST API] *********************************************************************************************

TASK [debug] ********************************************************************************************************
okay: [localhost] =>
    "msg": "“Let’s call an API”"

TASK [Everyone loves a very good Chuck Norris joke] **********************************************************************
okay: [localhost]

TASK [debug] ********************************************************************************************************
okay: [localhost] =>
    "results.json.value": "Chuck Norris is the only computer system that beats a Mac or a PC. Too bad all it does is round house kicks the user."

PLAY RECAP **********************************************************************************************************
localhost                  : okay=three    modified=zero    unreachable=zero    failed=zero    skipped=zero    rescued=zero    ignored=zero  

Next steps

Things can get far more difficult than I’ve proven right here. To get extra particulars, head over to Ansible’s documentation.

Most Popular

To Top