Bookmark this page

Guided Exercise: Managing the Boot Process and Scheduled Processes

In this exercise, you manage the startup process, schedule recurring jobs, and reboot managed hosts.

Outcomes

  • Schedule a Cron job.

  • Remove a single, specific Cron job from a crontab file.

  • Schedule an at task.

  • Set the default boot target on managed hosts.

  • Reboot managed hosts.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available, including the /home/student/system-process project directory.

[student@workstation ~]$ lab start system-process

Procedure 9.3. Instructions

  1. Change into the /home/student/system-process directory.

    [student@workstation ~]$ cd ~/system-process
    [student@workstation system-process]$
  2. Create the create_crontab_file.yml playbook in the working directory.

    Configure the playbook to use the ansible.builtin.cron module to create a crontab file named /etc/cron.d/add-date-time that schedules a recurring Cron job. The job should run as the devops user every two minutes starting at 09:00 and ending at 16:59 from Monday through Friday. The job should append the current date and time to the /home/devops/my_datetime_cron_job file.

    1. Create the create_crontab_file.yml playbook and add the lines needed to start the play. It should target the managed hosts in the webservers group and enable privilege escalation.

      ---
      - name: Recurring cron job
        hosts: webservers
        become: true
    2. Define a task that uses the ansible.builtin.cron module to schedule a recurring Cron job, the date >> /home/devops/my_date_time_cron_job command.

      Note

      The ansible.builtin.cron module provides a name option to uniquely describe the crontab file entry and to ensure expected results.

      The value you use for the name option is added to the crontab file as a comment. For example, the name option is required if you are removing a crontab entry using state: absent.

      Additionally, the name option prevents a new crontab entry from always being created when the default state, state: present, is set.

        tasks:
          - name: Crontab file exists
            ansible.builtin.cron:
              name: Add date and time to a file
              job: date >> /home/devops/my_date_time_cron_job
    3. Configure the job to run every two minutes starting at 09:00 and ending at 16:59 on Monday through Friday.

              minute: "*/2"
              hour: 9-16
              weekday: 1-5
    4. Use the cron_file parameter to use the crontab file named /etc/cron.d/add-date-time instead of an individual user's crontab in /var/spool/cron/.

      A relative path places the file in the /etc/cron.d directory.

      If the cron_file parameter is used, you must also specify the user parameter for the system crontab file. Use the devops user for this job.

              user: devops
              cron_file: add-date-time
              state: present
    5. When completed, the playbook must appear as follows. Review the playbook for accuracy.

      ---
      - name: Recurring cron job
        hosts: webservers
        become: true
      
        tasks:
          - name: Crontab file exists
            ansible.builtin.cron:
              name: Add date and time to a file
              job: date >> /home/devops/my_date_time_cron_job
              minute: "*/2"
              hour: 9-16
              weekday: 1-5
              user: devops
              cron_file: add-date-time
              state: present
    6. Run the ansible-navigator run --syntax-check command to verify the playbook syntax. Correct any errors before moving to the next step.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout create_crontab_file.yml --syntax-check
      playbook: /home/student/system-process/create_crontab_file.yml
    7. Run the create_crontab_file.yml playbook.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout create_crontab_file.yml
      
      PLAY [Recurring cron job] *************************************************
      
      TASK [Gathering Facts] ****************************************************
      ok: [servera.lab.example.com]
      
      TASK [Crontab file exists] ************************************************
      changed: [servera.lab.example.com]
      
      PLAY RECAP ****************************************************************
      servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    8. Run the following command to verify that the /etc/cron.d/add-date-time cron file exists, and its content is correct.

      [student@workstation system-process]$ ssh devops@servera \
      > "cat /etc/cron.d/add-date-time"
      #Ansible: Add date and time to a file
      */2 9-16 * * 1-5 devops date >> /home/devops/my_date_time_cron_job
  3. Create the remove_cron_job.yml playbook in the working directory. Configure the playbook to use the ansible.builtin.cron module to remove the Add date and time to a file Cron job from the /etc/cron.d/add-date-time crontab file.

    1. Create the remove_cron_job.yml playbook and add the following lines:

      ---
      - name: Remove scheduled cron job
        hosts: webservers
        become: true
      
        tasks:
          - name: Cron job removed
            ansible.builtin.cron:
              name: Add date and time to a file
              user: devops
              cron_file: add-date-time
              state: absent
    2. Run the ansible-navigator run --syntax-check command to verify the playbook syntax. Correct any errors before moving to the next step.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout remove_cron_job.yml --syntax-check
      playbook: /home/student/system-process/remove_cron_job.yml
    3. Run the remove_cron_job.yml playbook.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout remove_cron_job.yml
      
      PLAY [Remove scheduled cron job] ******************************************
      
      TASK [Gathering Facts] ****************************************************
      ok: [servera.lab.example.com]
      
      TASK [Cron job removed] ***************************************************
      changed: [servera.lab.example.com]
      
      PLAY RECAP ****************************************************************
      servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    4. Run the following command to verify that the /etc/cron.d/add-date-time file has been removed.

      [student@workstation system-process]$ ssh devops@servera \
      > "ls -l /etc/cron.d"
      total 4
      -rw-r--r--. 1 root root 128 Aug  9  2021 0hourly
  4. Create the schedule_at_task.yml playbook in the working directory. Configure the playbook to use the ansible.posix.at module to schedule a task that runs one minute in the future.

    The task should run the date command and redirect its output to the /home/devops/my_at_date_time file.

    Use the unique: yes option to ensure that if the command already exists in the at queue, a new task is not added.

    1. Create the schedule_at_task.yml playbook and add the following lines:

      ---
      - name: Schedule at task
        hosts: webservers
        become: true
        become_user: devops
      
        tasks:
          - name: Create date and time file
            ansible.posix.at:
              command: date > ~/my_at_date_time
              count: 1
              units: minutes
              unique: yes
              state: present
    2. Run the ansible-navigator run --syntax-check command to verify the playbook syntax. Correct any errors before moving to the next step.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout schedule_at_task.yml --syntax-check
      playbook: /home/student/system-process/schedule_at_task.yml
    3. Run the schedule_at_task.yml playbook.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout schedule_at_task.yml
      
      PLAY [Schedule at task] ***************************************************
      
      TASK [Gathering Facts] ****************************************************
      ok: [servera.lab.example.com]
      
      TASK [Create date and time file] ******************************************
      changed: [servera.lab.example.com]
      
      PLAY RECAP ****************************************************************
      servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    4. After waiting a minute or two for the at command to complete, run the following commands to verify that the /home/devops/my_at_date_time file exists and has the correct contents.

      [student@workstation system-process]$ ssh devops@servera \
      > "ls -l my_at_date_time"
      -rw-rw-r--. 1 devops devops 32 Aug 15 00:00 my_at_date_time
      
      [student@workstation system-process]$ ssh devops@servera \
      > "cat my_at_date_time"
      Mon Aug 15 12:00:00 AM EDT 2022
  5. Create the set_default_boot_target_graphical.yml playbook in the working directory. Write a play in the playbook to set the default systemd target to graphical.target.

    1. Create the set_default_boot_target_graphical.yml playbook and add the following lines:

      ---
      - name: Change default boot target
        hosts: webservers
        become: true
        gather_facts: false
        vars:
          default_target: "graphical.target"
      
        tasks:
          - name: Get current boot target
            ansible.builtin.command:
              cmd: systemctl get-default
            changed_when: false
            register: target
      
          - name: Set default boot target
            ansible.builtin.command:
              cmd: systemctl set-default {{ default_target }}
            when: default_target not in target['stdout']
    2. Run the ansible-navigator run --syntax-check command to verify the playbook syntax. Correct any errors before moving to the next step.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout set_default_boot_target_graphical.yml --syntax-check
      playbook: /home/student/system-process/set_default_boot_target_graphical.yml
    3. Before running the playbook, run the following command to verify that the current default boot target is multi-user.target.

      [student@workstation system-process]$ ssh devops@servera \
      > "systemctl get-default"
      multi-user.target
    4. Run the set_default_boot_target_graphical.yml playbook.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout set_default_boot_target_graphical.yml
      
      PLAY [Change default boot target] *****************************************
      
      TASK [Get current boot target] ********************************************
      ok: [servera.lab.example.com]
      
      TASK [Set default boot target] ********************************************
      changed: [servera.lab.example.com]
      
      PLAY RECAP ****************************************************************
      servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    5. Run the following command to verify that the default boot target is now graphical.target.

      [student@workstation system-process]$ ssh devops@servera \
      > "systemctl get-default"
      graphical.target
  6. Create the reboot_hosts.yml playbook in the working directory to reboot the managed hosts.

    1. Create the reboot_hosts.yml playbook and add the following lines:

      ---
      - name: Reboot hosts
        hosts: webservers
        become: true
      
        tasks:
          - name: Hosts are rebooted
            ansible.builtin.reboot:
    2. Run the ansible-navigator run --syntax-check command to verify the playbook syntax. Correct any errors before moving to the next step.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout reboot_hosts.yml --syntax-check
      playbook: /home/student/system-process/reboot_hosts.yml
    3. Before running the playbook, run the following command to determine the time stamp of the last system reboot:

      [student@workstation system-process]$ ssh devops@servera \
      > "who -b"
               system boot  2022-08-15 00:07
    4. Run the reboot_hosts.yml playbook.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout reboot_hosts.yml
      
      PLAY [Reboot hosts] *******************************************************
      
      TASK [Gathering Facts] ****************************************************
      ok: [servera.lab.example.com]
      
      TASK [Hosts are rebooted] *************************************************
      changed: [servera.lab.example.com]
      
      PLAY RECAP ****************************************************************
      servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    5. Run the following command to determine the time stamp of the last system boot. The time stamp displayed after the playbook runs should be later.

      [student@workstation system-process]$ ssh devops@servera \
      > "who -b"
               system boot  2022-08-15 00:44
    6. Run this next command to determine that the graphical.target boot target is still the default after the reboot.

      [student@workstation system-process]$ ssh devops@servera \
      > "systemctl get-default"
      graphical.target
  7. To maintain consistency throughout the remaining exercises, change the default boot target back to its former setting, multi-user.target.

    Copy your set_default_boot_target_graphical.yml playbook to set_default_boot_target_multi-user.yml in the Ansible project directory.

    Edit the default_target variable to set multi-user.target as the default.

    1. Copy your set_default_boot_target_graphical.yml playbook to set_default_boot_target_multi-user.yml.

      [student@workstation system-process]$ cp set_default_boot_target_graphical.yml \
      > set_default_boot_target_multi-user.yml
    2. Edit the play in the set_default_boot_target_multi-user.yml playbook to change the default_target variable to multi-user.target.

      ---
      - name: Change default boot target
        hosts: webservers
        become: true
        gather_facts: false
        vars:
          default_target: "multi-user.target"
      
        tasks:
          - name: Get current boot target
            ansible.builtin.command:
              cmd: systemctl get-default
            changed_when: false
            register: target
      
          - name: Set default boot target
            ansible.builtin.command:
              cmd: systemctl set-default {{ default_target }}
            when: default_target not in target['stdout']
    3. Run the ansible-navigator run --syntax-check command to verify the playbook syntax. Correct any errors before moving to the next step.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout set_default_boot_target_multi-user.yml --syntax-check
      playbook: /home/student/system-process/set_default_boot_target_multi-user.yml
    4. Run the set_default_boot_target_multi-user.yml playbook.

      [student@workstation system-process]$ ansible-navigator run \
      > -m stdout set_default_boot_target_multi-user.yml
      
      PLAY [Change default boot target] *****************************************
      
      TASK [Get current boot target] ********************************************
      ok: [servera.lab.example.com]
      
      TASK [Set default boot target] ***********************************
      changed: [servera.lab.example.com]
      
      PLAY RECAP ****************************************************************
      servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    5. Run the following command to verify that the default boot target is now multi-user.target.

      [student@workstation system-process]$ ssh devops@servera \
      > "systemctl get-default"
      multi-user.target

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 system-process

This concludes the section.

Revision: rh294-9.0-c95c7de