Bookmark this page

Scheduling Jobs and Configuring Notifications

Objectives

  • Schedule automatic job execution and configure job notifications.

Scheduling Job Execution

Sometimes you might need to launch a job template automatically at a particular time, or on a particular schedule. You can use automation controller to configure schedules that launch job templates on a customizable schedule.

If you have the Execute role on a job template, then you can schedule the job template to run at a specific date, time, and frequency. To create a new job template schedule, navigate to ResourcesTemplates and click the job template that you want to schedule. Click the Schedules tab and then click Add to create a new schedule for that job template.

Enter the required details:

Name

The name of the schedule.

Start date/time

The date and time that the job schedule starts.

Local time zone

You can use the timedatectl command to determine your local time zone in this format.

Run frequency

How often to repeat the associated job. You can choose None (run once), Minute, Hour, Day, Week, Month, or Year.

Depending on the chosen frequency, you might need to provide additional information (for example, to launch a job every two days, or on the first Sunday of every month).

When finished, click Save to save the schedule. You can activate or deactivate a schedule by setting the On/Off switch at the top of the Details page for the schedule.

Figure 5.6: Scheduling job execution

Temporarily Disabling a Schedule

Navigate to ViewsSchedules in the automation controller web UI to open the Schedules page. This page lists all job schedules. Set the On/Off switch under the Actions column of the desired schedule name to activate or deactivate the schedule.

You can also edit or delete any schedule, assuming you have sufficient privileges to do so, from this page.

Figure 5.7: Deactivated and activated schedules

Scheduled Management Jobs

Automation controller ships with four scheduled management jobs by default. These schedules run built-in management jobs to perform periodic maintenance on the automation controller.

Management jobDescription
Cleanup Job Schedule Removes information about jobs that are more than 120 days old. It runs weekly on Sundays.
Cleanup Activity Schedule Removes information from the activity stream that is more than 355 days old. It runs weekly on Tuesdays.
Cleanup Expired OAuth 2 Tokens Deletes the expired OAuth 2 access and refresh tokens. It runs weekly.
Cleanup Expired Sessions Removes expired browser sessions. It runs weekly.

You can change the frequency of these management jobs by editing their schedules.

Reporting Job Execution Results

Red Hat Ansible Automation Platform provides centralized logging and auditing. When you execute a job, details about the job execution are logged in the automation controller database. You can use this database to review job execution history.

Historical job execution details are helpful for confirming the success or failure of scheduled and delegated job executions. The ability to retrieve historical job execution details is helpful, but for jobs related to critical functions, you might prefer immediate notification of when a job starts, as well as the success or failure of its execution.

Automation controller can send notifications when a job starts, as well as the job execution results. You need to create notification templates that specify how to send notifications to take advantage of this feature. Automation controller supports many mechanisms for sending notifications. Some are based on open protocols, such as email and IRC, and others are based on proprietary solutions, such as Slack and Twilio.

Notification Templates

Notification templates help you to send notifications on job start, job success, and job failure, even in events related to approvals, or any combination thereof.

Notification templates specify how to send notifications. The following notification types are supported:

  • Email

  • Grafana

  • IRC

  • Mattermost

  • PagerDuty

  • Rocket.Chat

  • Slack

  • Twilio

  • Webhook

Creating Notification Templates

Use the following procedure to create a notification template:

  1. Navigate to AdministrationNotifications in the automation controller web UI.

  2. Click Add to create a notification template, and enter the required details:

    Name

    The name of the notification template.

    Description

    Enter a description for the notification template. (Optional)

    Organization

    The organization within which to create the notification template.

    Type

    The mechanism to be used by the notification template for generating notifications.

  3. Enter the required details for the notification template type in the Type Details section.

    For example, for the E-mail notification template type, the following fields are displayed in the Type details section:

    Username

    Specifies the username when using SMTP authentication. (Optional)

    Password

    Specifies the password for the username when using SMTP authentication. (Optional)

    Host

    Specifies which SMTP server to use.

    Recipient list

    Specifies the email addresses of the recipients for the notification email, one per line.

    Sender e-mail

    Specifies the email address of the sender when composing the notification.

    Port

    Specifies the port to connect to on the SMTP server.

    Timeout

    Specifies the maximum time in seconds (from 1 to 120) that the automation controller attempts connecting to the SMTP server.

    E-mail options

    You can enable SSL or TLS to secure email communications by selecting the respective checkbox. (Optional)

  4. Set the Customize messages switch to on if you want to customize the content of the notification messages.

    For each of the notification types, preconfigured messages are available that use Jinja2 expressions to provide information about the job. The preconfigured messages use variables related to the job variable (such as job_friendly_name for the friendly name of the job, job_metadata for the job metadata, and so on), and some of its attributes (such as name for the name of the job, status for the status of the job, and so on).

    For example, for the E-mail notification template type, the preconfigured start message body uses the friendly name of the job, the id and status job attributes, as well as the URL and all metadata information for the job:

    {{ job_friendly_name }} #{{ job.id }} had status {{ job.status }}, view details at {{ url }}
    
    {{ job_metadata }}

    You can create custom notifications by modifying the preconfigured messages. A link to a complete list of supported attributes and variables is available in the references at the end of this section.

    Figure 5.8: Custom messages for Email type
  5. Click Save to finalize the creation of the notification template.

  6. You can send a test notification for each notification template after creating or editing them. Click the Test notification icon in the Actions column of the desired notification template. The Status column should display Successful.

Figure 5.9: Testing notification templates

Enabling Job Result Notification

When you create a notification template, it becomes available for use by the following automation controller resources:

  • Job templates

  • Projects

  • Inventory sources

  • Workflow templates

  • Organizations

For job templates, projects, and inventory source, the available notifications are Start, Success and Failure. For workflow templates and organizations, you can also get Approval notifications for events with approvals. This is discussed later in the course.

Figure 5.10: Enabling notifications on a job template

Use the following procedure to enable notifications for a job template:

  1. Navigate to ResourcesTemplates in the automation controller web UI.

  2. Click the name of the required job template and then click the Notifications tab to display the list of notification templates.

  3. Each listed notification template has switches for controlling Start, Success, and Failure notifications. Set the corresponding switches to configure the desired notifications for the job template.

Warning

Under some circumstances, you might encounter an issue in which automation controller fails to send a notification. This is a known issue at the time of writing, caused by a race condition related to https://github.com/ansible/awx/issues/11422.

One symptom, besides missing notification messages, is content similar to the following in the /var/log/tower/task_system.log file:

2022-02-17 06:54:54,645 WARNING  [b8c34f2e75d2430c8874f3f8feb557cc] awx.main.tasks Failed to even try to send notifications for job '2022-02-17 06:37:20.281267+00:00-56302-running' due to job not being in finished state.

Automation controller in Red Hat Ansible Automation Platform 2.1.2 and later includes a setting that you can adjust to work around the issue.

By default, automation controller uses the setting AWX_NOTIFICATION_JOB_FINISH_MAX_RETRY = 5. If automation controller fails to send a notification, then you can change this variable to have a higher value, such as 20. Modify the /var/lib/awx/venv/awx/lib/python3.9/site-packages/awx/settings/defaults.py file on the automation controller. After making this change, run the automation-controller-service restart command.

Revision: do467-2.2-08877c1