Bookmark this page

Guided Exercise: Creating Job Templates and Launching Jobs

  • Create a job template, assign a role to a team so that team members can use that job template, and use that job template to launch a job.

Outcomes

  • Create a job template.

  • Assign roles to the job template.

  • Launch a job from the automation controller web UI.

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.

[student@workstation ~]$ lab start provision-job

Procedure 4.2. Instructions

  1. Review the automation controller resources created by the lab command for this exercise.

    1. Navigate to https://controller.lab.example.com and log in as the admin user with redhat as the password.

    2. Navigate to ResourcesProjects and then click the DB Project link. This is the Git project that contains the playbooks to set up the database servers.

    3. Review the Source Control URL and Source Control Credential fields.

    4. Navigate to ResourcesCredentials and then click the link for the Developers credential. This is the machine credential used for running tasks on the hosts.

    5. Navigate to ResourcesInventories and then click the DB Inventory link. This is the inventory for the database servers.

    6. Review the Groups and Hosts tabs. Notice that this inventory has one host group, db_servers, and one host, serverd.lab.example.com.

  2. Create a new job template to set up the database servers.

    1. Navigate to ResourcesTemplates and then click AddAdd job template to open the Create New Job Template page.

    2. Enter the following details and then click Save:

      FieldValue
      Name Database setup
      Description Setup database servers
      Job Type Run
      Inventory DB Inventory
      Project DB Project
      Playbook create_samples_db.yml
      Credentials Developers
  3. Assign the Admin role to the Developers team on the Database setup job template.

    1. Navigate to ResourcesTemplates and then click the Database setup job template link.

    2. Click the Access tab to manage the permissions for the Database setup job template.

    3. Click Add to open the Add Roles dialog box.

    4. Click Teams and then click Next to display the list of available teams.

    5. Select the Developers team, and then click Next to display the possible roles to apply.

    6. Select the Admin role and then click Save. This redirects you to the list of permissions for the Database setup job template, which now shows that all members of the Developers team are assigned the Admin role on the Database setup job template.

  4. Run the Database setup job template as the user daniel, who is a member of the Developers team. This attempt to run the job template fails. You examine the cause of failure in the next step.

    1. Log out as the admin user and log in as the daniel user with redhat123 as the password.

    2. Navigate to ResourcesTemplates and then click the Launch Template icon for the Database setup job template.

    3. The job fails. Review the error message in the output.

      ERROR! couldn't resolve module/action 'mysql_db'. This often indicates a misspelling, missing collection, or incorrect module path.
      ...output omitted...
    4. Log out of the automation controller web UI.

  5. Review the create_samples_db.yml playbook and examine the reason the job failed.

    1. 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/
    2. Clone the https://git.lab.example.com/git/my_dbservers.git Git repository into the /home/student/git-repos directory.

      [student@workstation git-repos]$ git clone \
      > https://git.lab.example.com/git/my_dbservers.git
      Cloning into 'my_dbservers'...
      ...output omitted...
      [student@workstation git-repos]$ cd my_dbservers
    3. Review the content of the create_samples_db.yml playbook.

      ---
      - name: Install and create a sample database
        hosts: all
        become: true
        gather_facts: false
      
        tasks:
          - name: Ensure MariaDB is installed
            yum:
              name:
                - mariadb
                - mariadb-server
                - python3-PyMySQL
              state: present
      
          - name: Ensure the mariadb service is started and enabled
            systemd:
              name: mariadb
              state: started
              enabled: true
      
          - name: Ensure the samples database exists
            mysql_db:
              name: samples
              state: present
    4. In the step 4.3, the error indicated that the job failure was related to the mysql_db module. The mysql_db module is part of the community.mysql collection, which is not part of the ee-supported-rhel8 execution environment. It was included in Ansible 2.9, so it is part of the ee-29-rhel8 execution environment. This playbook was probably originally written for Ansible 2.9.

  6. Determine which execution environment the Database setup job template is using.

    1. Navigate to https://controller.lab.example.com and log in as the admin user with redhat as the password.

    2. Navigate to ResourcesTemplates and then click the Database setup job template link.

    3. Click the Automation Hub Default execution environment link.

    4. The Details page indicates that the execution environment is using the hub.lab.example.com/ee-supported-rhel8:latest image. The community.mysql collection is not included in this execution environment.

  7. To resolve this issue, modify the Database setup job template to use the Ansible Engine 2.9 execution environment execution environment. This execution environment provides the modules available in Ansible 2.9, including the mysql_db module.

    1. Navigate to ResourcesTemplates and then click the Edit Template icon for the Database setup job template.

    2. Click the search icon for Execution Environment.

    3. Select Ansible Engine 2.9 execution environment and then click Select.

    4. Click Save to save the changes to the job template.

      Note

      An alternative solution that allows you to continue using the later execution environment is to modify the project to include or retrieve the missing community.mysql Ansible Content Collection, so that it is available to the execution environment.

      You could embed the collection in the project’s Git repository, or you could use a requirements.yml file to have the automation controller automatically pull it from Ansible Galaxy. For this to work, you need to make sure the execution environment has all the prerequisites needed by that collection already installed.

      You also need to modify the playbook so that instead of calling the module as mysql_db, you call it by its fully qualified collection name, community.mysql.mysql_db.

  8. Rerun the Database setup job template as the daniel user.

    1. Navigate to https://controller.lab.example.com, log out as the admin user, and log in as the daniel user with redhat123 as the password.

    2. Navigate to ResourcesTemplates and then click the Launch Template icon for the Database setup job template. After some time, the job launched by the job template succeeds.

  9. Confirm that the job template correctly configured a MariaDB database on the serverd.lab.example.com server.

    1. Open a terminal on the workstation machine, connect to the serverd machine, and become the root user.

      [student@workstation my_dbservers]$ ssh serverd
      ...output omitted...
      [student@serverd ~]$ sudo -i
      [sudo] password for student: student
      [root@serverd ~]#
    2. Confirm that the samples database exists.

      [root@serverd ~]# mysql -e "SHOW DATABASES"
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | mysql              |
      | performance_schema |
      | samples            |
      +--------------------+
    3. Log out of the serverd machine.

      [root@serverd ~]# exit
      logout
      [student@serverd ~]$ exit
      logout
      Connection to serverd closed.
      [student@workstation my_dbservers]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish provision-job

This concludes the section.

Revision: do467-2.2-08877c1