Launch a job from an existing job template by running an Ansible Playbook.
Outcomes
Launch a job from an existing job template by running an Ansible Playbook.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that automation controller is installed and configured with any necessary resources created in previous exercises. It also creates additional Vault credentials and uploads a new playbook into the Git repository.
[student@workstation ~]$ lab start code-api
Procedure 8.3. Instructions
In a web browser, log in to automation controller as admin and set the fact cache timeout to one day.
Navigate to https://controller.lab.example.com and log in as the admin user with redhat as the password.
Navigate to , and on the section click to open the page. 1.3) Scroll down and click .
Set to 86400 seconds (one day), and then click .
Navigate to https://controller.lab.example.com/api/ to examine the browsable API on that automation controller, using a new tab in your web browser.
Open a new tab in your web browser, and navigate to https://controller.lab.example.com/api/. You should see the browsable API for automation controller. Because you logged in as admin on this web browser and have not logged out, you are authenticated to the API as the admin user.
Click one of the link to access the browsable list of all the available resources accessible throughout the API.
From the list, click the link to access that URI. On the instances object for the controller.lab.example.com node you can see the heartbeat time stamp value.
An external program can use this URI to verify that the automation controller server is operating.
...output omitted...
"instances": [
{
"node": "controller.lab.example.com",
"node_type": "hybrid",
"uuid": "0230c1cb-5f20-4db4-b186-394bc8e9ccea",
"heartbeat": "2022-06-14T17:07:55.992650Z",
"capacity": 36,
"version": "4.2.0"
}
...output omitted...In your web browser, use the API to launch a job from the existing Refresh Fact Cache job template.
Click the link at the top of the page to return to the URI.
Click the link to access the list of available job templates.
From the list of results, locate the job template containing "name": "Refresh Fact Cache". Scroll up to find its url resource and click the link to access the template. The link should be or similar. (The number at the end of the link might be a different number.)
Look for the job template’s related resource named launch. Click the link associated with that resource. The link should be or similar.
...output omitted...
"schedules" : "/api/v2/job_templates/11/schedules/",
"launch" : "/api/v2/job_templates/11/launch/",
"notification_templates_success" : "/api/v2/job_templates/11/notification_templates_success/"
...output omitted...This link sends a GET request to the launch resource for that job template, providing details about what information is needed to launch a job from the template.
To watch the execution of the job you are about to launch, return to the automation controller web UI tab. Click → in the navigation bar.
Go back to the https://controller.lab.example.com/api/v2/job_templates/11/launch/ tab.
Go to the bottom of the page and click to launch the job.
The page immediately refreshes with JSON information about the launched job, including the job id number.
Quickly switch to the tab displaying the page in the automation controller web UI.
The launched job should be at the top of the list of jobs that have been run. You can confirm it is the job you launched by matching its in the web UI on this tab with the id in the JSON output for the job on the other tab.
Use curl to launch a job from an existing job template through the API.
Search for the Refresh Fact Cache job template by using the API with a name filter. Determine the ID of the job template. This ID should be the same as the one that you saw using your web browser earlier in this exercise.
[student@workstation ~]$curl -X GET --user admin:redhat \>https://controller.lab.example.com/api/v2/job_templates/?name="Refresh Fact Cache" \>-k -s | jq
In the preceding command, the -k option instructs curl to operate even if it cannot verify the SSL certificate. The -s option instructs curl to only display the returned data; it does not display progress or error messages.
"survey_spec" : "/api/v2/job_templates/11/survey_spec/","launch" : "/api/v2/job_templates/11/launch/","activity_stream" : "/api/v2/job_templates/11/activity_stream/", "jobs" : "/api/v2/job_templates/11/jobs/",
Now that you have confirmed the ID for the Refresh Fact Cache job template, use that number and the job template’s launch resource to get information about how to launch the job.
[student@workstation ~]$curl -X GET --user admin:redhat \>https://controller.lab.example.com/api/v2/job_templates/>11/launch/ -k -s \| jq{ "passwords_needed_to_start" : [], "ask_limit_on_launch" : false, "ask_inventory_on_launch" : false, "can_start_without_user_input" : true, "defaults" : { ...output omitted...
Because you can launch this job without user input, you can issue a POST request to the same URL to launch the job, without providing any additional data.
[student@workstation ~]$curl -X POST --user admin:redhat \>https://controller.lab.example.com/api/v2/job_templates/>11/launch/ -k -s \| jq{ ...output omitted... "unified_job_template" : 11, "skip_tags" : "", "execution_environment" : 3, "job_type" : "run", "forks" : 0, "job_slice_number" : 0, "job_template" : 11, "extra_vars" : "{}", "created" : "2022-06-17T20:35:38.191129Z", ...output omitted... "use_fact_cache" : true, "result_traceback" : "", "artifacts" : {}, "job_env" : {}, "project" : 10,"id" : 7,"ignored_fields" : {}, "job" : 7, "job_cwd" : "", ...output omitted... }
Pay attention to the id number for the launched job in the JSON output.
Return to the automation controller web UI tab that displays the page.
The job launched by the curl command should now be at the top of the list of jobs that have run. Again, you can confirm that it is the job you just launched by comparing the ID in the web UI with the id you just saw in the JSON output from the curl command.
You have been provided with a playbook named api_controller.yml that uses the automation controller API to launch a job from an existing job template. Look at the playbook to see how it works.
The username and password to access the API are stored in this playbook, and so it is encrypted using the ansible-vault command, with a password of redhat.
From a terminal, create the /home/student/git-repos directory if it does not already exist, and then change into it.
[student@workstation ~]$mkdir -p ~/git-repos/[student@workstation ~]$cd ~/git-repos/
Clone the https://git.lab.example.com/git/internal_web.git repository, which contains the api_controller.yml playbook, to the /home/student/git-repos directory.
[student@workstation git-repos]$git clone \>https://git.lab.example.com/git/internal_web.gitCloning into 'internal_web'... ...output omitted... [student@workstation git-repos]$cd internal_web
Inspect the contents of the api_controller.yml playbook. The contents of the file are encrypted:
[student@workstation internal_web]$ cat api_controller.yml
$ANSIBLE_VAULT;1.1;AES256
62633766376433336366313731643761663863656339623938646234386334633764646436626363
3065366133316630616164343461366465323833626631660a373165303136383236653033613663
31646132653439646464363735666236653636393761336632326138373137343438626430663039
3165356237326236310a373034323434336364383037623536623961356263333863396334303163
...output omitted...You can use the ansible-vault view command to temporarily decrypt and display the contents of the playbook:
[student@workstation internal_web]$ansible-vault view api_controller.ymlVault password:redhat...output omitted... vars: controller_user: admin controller_pass: redhat controller_host: controller.lab.example.com tasks: - name: Launch Job uri: url: https://{{ controller_host }}/api/v2/job_templates/Internal%20Web%20Access/launch/ method: POST validate_certs: no return_content: yes user: "{{ controller_user }}" password: "{{ controller_pass }}" force_basic_auth: yes status_code: 201 ...output omitted...
In automation controller, create a new Vault credential named Vault Usage that contains the password to decrypt the api_controller.yml playbook.
Click → and then click .
On the page, fill in the details as follows:
| Field | Value |
|---|---|
| Name |
Vault Usage
|
| Description |
To unlock api_controller.yml
|
| Credential Type | |
| Password |
redhat
|
Click .
A job template named API Usage that launches the api_controller.yml playbook has already been partially configured for you. Edit the job template to associate the Vault Usage credential with it so that automation controller can decrypt the playbook.
Navigate to → .
Click the icon for the job template.
Click the search icon under . In the window, click the list to access the list of available credential types. Choose from the list.
In the lower pane, select the Vault Usage credential name, and then click to add this new credential to the API Usage job template.
Click at the bottom of the page.
Launch a new job based on the modified API Usage job template.
Navigate to → .
Launch the job by clicking the icon for the job template.
Observe the output of the job. The Print Output task shows how the automation controller API was used by the job playbook to launch a second job.
Click in the navigation bar.
A new job named Internal Web Access has been launched. This new job was launched by the job you launched using the API Usage job template.