Good morning! Today, we’ll work on accessing a TV show quotes API to get random quotes from various shows. We will use the uri module to interact with the API and print out the results.
First, let’s start by getting our environment ready:
student@bchd~$ vim tvquotes.yml
---
- name: TV Quotes Playbook
hosts: localhost
vars:
baseurl: "https://quotes.alakhpc.com/quotes"
tasks:
- name: Get a random quote from the API
uri:
url: "{{ baseurl }}?show=Frasier"
register: quote_response
- name: Print the whole quote response
debug:
var: quote_responseRun your playbook:
student@bchd~$ ansible-playbook tvquotes.yml
Check out the query parameters that this API supports:
| Query Parameter | Description | Example Usage | Example Response |
|---|---|---|---|
show |
Specifies the TV show from which you want a random quote (see below for available shows) | https://quotes.alakhpc.com/quotes?show=Friends |
{ "show": "Friends", "character": "Chandler", "text": "Could I *be* wearing any more clothes?" } |
short |
Limits the response to only one-line quotes when set to true. Defaults to false. |
https://quotes.alakhpc.com/quotes?show=Breaking+Bad&short=true |
{ "show": "Breaking Bad", "character": "Walter White", "text": "Say my name." } |
Click here to see the list of available TV shows:
Woah, what's up with all those
%20s? That's because you can't use spaces in URLs. White spaces are encoded as%20so browsers can interpret them properly!
How%20I%20Met%20Your%20MotherThe%20MiddleNew%20GirlSuits3rd%20Rock%20from%20the%20SunArrested%20DevelopmentMalcolm%20in%20the%20MiddleMonkThe%20Fresh%20Prince%20of%20Bel-AirParks%20And%20RecreationHome%20ImprovementCheersModern%20FamilySeinfeldThe%20OfficeThe%20GoldbergsGilmore%20GirlsFrasierBreaking%20BadScrubsBoy%20Meets%20WorldEverybody%20Loves%20RaymondThe%20Good%20PlaceBrooklyn%20Nine-NineEverybody%20Hates%20ChrisLuciferSchitt's%20CreekDerry%20GirlsFriendsStranger%20ThingsThe%20Golden%20Girls
Right now, this playbook will always return quotes from the show Frasier (a Chad favorite ❤️). Edit the playbook so that:
Gimme a hint!
- Hint 1: You’ll need to remove the hard-coded show name and replace it with a variable.
Need another hint?
- Hint 2: Define a variable, for example,
show_nameand set it to the show you want. Then use{{ show_name }}in the URL instead of the hard-coded "Frasier."
Want another hint?
- Hint 3: Remember that query parameters go at the end of the URL, like this:
?show={{ show_name }}.
Want the solution?
vars:
baseurl: "https://quotes.alakhpc.com/quotes"
show_name: "Friends" # Replace "Friends" with any other show name
tasks:
- name: Get a random quote from a specific show
uri:
url: "{{ baseurl }}?show={{ show_name }}"
register: quote_responseGimme a hint!
- Hint 1: The
shortparameter is another query parameter that should be added to the end of the URL.
Need another hint?
- Hint 2: Add the
shortparameter with a default valuefalsein the query string like you did with the show name.
Want another hint?
- Hint 3: The URL should look like this:
?show={{ show_name }}&short=false.
Want the solution?
vars:
baseurl: "https://quotes.alakhpc.com/quotes"
show_name: "Friends" # Variable for the show
tasks:
- name: Get a random quote with the short parameter
uri:
url: "{{ baseurl }}?show={{ show_name }}&short=false"
register: quote_response3. If you have time, define the value of the short parameter as a variable as well. Set it to "false" by default. It is IMPORTANT that you use "quotes"! (you'll see why)
Gimme a hint!
- Hint 1: To make
shortconfigurable, declare it as a variable in the playbook'svarssection.
Need another hint?
- Hint 2: Set the default value of
shorttofalsein thevarssection, just likeshow_name.
Want another hint?
- Hint 3: You should declare
shortas a variable like this:short: false.
Want the solution?
vars:
baseurl: "https://quotes.alakhpc.com/quotes"
show_name: "Friends"
short: false # Define the short parameter as a variable
tasks:
- name: Get a random quote with variables for both show and short
uri:
url: "{{ baseurl }}?show={{ show_name }}&short={{ short }}"
register: quote_response