In this exercise, you will manage the startup process, schedule recurring jobs, and reboot managed hosts.
Outcomes
You should be able to use a playbook to:
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.
Run the lab system-process start script from workstation to configure the environment for the exercise.
The script creates the system-process working directory, and downloads the Ansible configuration file and the host inventory file needed for the exercise.
[student@workstation ~]$lab system-process start
Procedure 9.3. Instructions
As the student user on workstation, change to the /home/student/system-process working directory.
[student@workstation ~]$cd ~/system-process[student@workstation system-process]$
Create a playbook, create_crontab_file.yml, in the current working directory.
Configure the playbook to use the cron module to create the /etc/cron.d/add-date-time crontab file that schedules a recurring cron job.
The job should run as the devops user every two minutes between 09:00 and 16:59 on Monday through Friday.
The job should append the current date and time to the file /home/devops/my_datetime_cron_job
Create a new playbook, create_crontab_file.yml, 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
Define a task that uses the cron module to schedule a recurring cron job.
The cron module provides a name option to uniquely describe the crontab file entry and to ensure expected results.
The description is added to the crontab file.
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
cron:
name: Add date and time to a fileConfigure the job to run every two minutes between 09:00 and 16:59 on Monday through Friday.
minute: "*/2"
hour: 9-16
weekday: 1-5Use the cron_file parameter to use the /etc/cron.d/add-date-time crontab file instead of an individual user's crontab in /var/spool/cron/.
A relative path will place the file in /etc/cron.d directory.
If the cron_file parameter is used, you must also specify the user parameter.
user: devops
job: date >> /home/devops/my_date_time_cron_job
cron_file: add-date-time
state: presentWhen completed, the playbook should appear as follows. Review the playbook for accuracy.
---
- name: Recurring cron job
hosts: webservers
become: true
tasks:
- name: Crontab file exists
cron:
name: Add date and time to a file
minute: "*/2"
hour: 9-16
weekday: 1-5
user: devops
job: date >> /home/devops/my_date_time_cron_job
cron_file: add-date-time
state: presentVerify playbook syntax by running the ansible-playbook --syntax-check create_crontab_file.yml command.
Correct any errors before moving to the next step.
[student@workstation system-process]$ansible-playbook --syntax-check \>create_crontab_file.ymlplaybook: create_crontab_file.yml
Run the playbook.
[student@workstation system-process]$ansible-playbook create_crontab_file.ymlPLAY [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
Run an ad hoc command to verify that the /etc/cron.d/add-date-time cron file exists and its content is correct.
[student@workstation system-process]$ansible webservers -u devops -b \>-a "cat /etc/cron.d/add-date-time"servera.lab.example.com | CHANGED | rc=0 >> #Ansible: Add date and time to a file */2 9-16 * * 1-5 devops date >> /home/devops/my_date_time_cron_job
Create a playbook, remove_cron_job.yml, in the current working directory.
Configure the playbook to use the cron module to remove the Add date and time to a file cron job from the /etc/cron.d/add-date-time crontab file
Create a new playbook, remove_cron_job.yml, and add the following lines:
---
- name: Remove scheduled cron job
hosts: webservers
become: true
tasks:
- name: Cron job removed
cron:
name: Add date and time to a file
user: devops
cron_file: add-date-time
state: absentVerify playbook syntax by running the ansible-playbook --syntax-check remove_cron_job.yml command.
Correct any errors before moving to the next step.
[student@workstation system-process]$ansible-playbook --syntax-check \>remove_cron_job.ymlplaybook: remove_cron_job.yml
Run the playbook.
[student@workstation system-process]$ansible-playbook remove_cron_job.ymlPLAY [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
Run an ad hoc command to verify that the /etc/cron.d/add-date-time cron file continues to exist but the cron job has been removed.
[student@workstation system-process]$ansible webservers -u devops -b \>-a "cat /etc/cron.d/add-date-time"servera.lab.example.com | CHANGED | rc=0 >>
Create a playbook, schedule_at_task.yml, in the current working directory.
Configure the playbook to use the 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.
Create a new playbook, schedule_at_task.yml, and add the following lines:
---
- name: Schedule at task
hosts: webservers
become: true
become_user: devops
tasks:
- name: Create date and time file
at:
command: "date > ~/my_at_date_time\n"
count: 1
units: minutes
unique: yes
state: presentVerify playbook syntax by running the ansible-playbook -syntax-check schedule_at_task.yml command.
Correct any errors before moving to the next step.
[student@workstation system-process]$ansible-playbook --syntax-check \>schedule_at_task.ymlplaybook: schedule_at_task.yml
Run the playbook.
[student@workstation system-process]$ansible-playbook schedule_at_task.ymlPLAY [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
After waiting one minute for the at command to complete, run ad hoc commands to verify that the /home/devops/my_at_date_time file exists and has the correct contents.
[student@workstation system-process]$ansible webservers -u devops \>-a "ls -l my_at_date_time"servera.lab.example.com | CHANGED | rc=0 >> -rw-rw-r--. 1 devops devops 30 abr 17 06:15 my_at_date_time[student@workstation system-process]$ansible webservers -u devops \>-a "cat my_at_date_time"servera.lab.example.com | CHANGED | rc=0 >> Thu Jul 22 13:24:34 PDT 2021
Create a playbook, set_default_boot_target_graphical.yml, in the current working directory.
Configure the playbook to use the file module to change the symbolic link on managed hosts to reference the graphical-target boot target.
In the following file module, the src parameter value is what the symbolic link references.
The dest parameter value is the symbolic link.
Create a new playbook, set_default_boot_target_graphical.yml, and add the following lines:
---
- name: Change default boot target
hosts: webservers
become: true
tasks:
- name: Default boot target is graphical
file:
src: /usr/lib/systemd/system/graphical.target
dest: /etc/systemd/system/default.target
state: linkVerify the playbook syntax by running the ansible-playbook --syntax-check set_default_boot_target_graphical.yml command.
Correct any errors before moving to the next step.
[student@workstation system-process]$ansible-playbook --syntax-check \>set_default_boot_target_graphical.ymlplaybook: set_default_boot_target_graphical.yml
Before running the playbook, run an ad hoc command to verify that the current default boot target is multi-user.target:
[student@workstation system-process]$ansible webservers -u devops -b \>-a "systemctl get-default"servera.lab.example.com | CHANGED | rc=0 >>multi-user.target
Run the playbook.
[student@workstation system-process]$ansible-playbook \>set_default_boot_target_graphical.ymlPLAY [Change default boot target] ***************************************** TASK [Gathering Facts] **************************************************** ok: [servera.lab.example.com] TASK [Default boot target is graphical] *********************************** changed: [servera.lab.example.com] PLAY RECAP **************************************************************** servera.lab.example.com : ok=2 changed=1 unreachable=0 failed=0
Run an ad hoc command to verify that the default boot target is now graphical.target.
[student@workstation system-process]$ansible webservers -u devops -b \>-a "systemctl get-default"servera.lab.example.com | CHANGED | rc=0 >>graphical.target
Create a playbook, reboot_hosts.yml, in the current working directory that reboots the managed hosts.
It is not required to reboot a server after changing the default target.
However, knowing how to create a playbook that reboots managed hosts may prove useful.
Create a new playbook, reboot_hosts.yml, and add the following lines:
---
- name: Reboot hosts
hosts: webservers
become: true
tasks:
- name: Hosts are rebooted
reboot:Verify the playbook syntax by running the ansible-playbook --syntax-check reboot_hosts.yml command.
Correct any errors before moving to the next step.
[student@workstation system-process]$ansible-playbook --syntax-check \>reboot_hosts.ymlplaybook: reboot_hosts.yml
Before running the playbook, run an ad hoc command to determine the timestamp of the last system reboot.
[student@workstation system-process]$ansible webservers -u devops -b \>-a "who -b"servera.lab.example.com | CHANGED | rc=0 >>system boot 2021-07-22 14:34
Run the playbook.
[student@workstation system-process]$ansible-playbook reboot_hosts.ymlPLAY [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
Run an ad hoc command to determine the timestamp of the last system reboot. The timestamp displayed after the playbook runs should be later.
[student@workstation system-process]$ansible webservers -u devops -b \>-a "who -b"servera.lab.example.com | CHANGED | rc=0 >>system boot 2021-07-22 14:52
Run a second ad hoc command to determine that the graphical.target boot target survived the reboot.
[student@workstation system-process]$ansible webservers -u devops -b \>-a "systemctl get-default"servera.lab.example.com | CHANGED | rc=0 >>graphical.target
To maintain consistency throughout the remaining exercises, change the default boot target back to its former setting, multi-user.target.
Create a playbook, set_default_boot_target_multi-user.yml, in the current working directory.
Configure the playbook to use the file module to change the symbolic link on managed hosts to reference the multi-user.target boot target.
Create a new playbook, set_default_boot_target_multi-user.yml, and add the following lines:
---
- name: Change default runlevel target
hosts: webservers
become: true
tasks:
- name: Default runlevel is multi-user target
file:
src: /usr/lib/systemd/system/multi-user.target
dest: /etc/systemd/system/default.target
state: linkVerify playbook syntax by running the ansible-playbook --syntax-check set_default_boot_target_multi-user.yml command.
Correct any errors before moving to the next step.
[student@workstation system-process]$ansible-playbook --syntax-check \>set_default_boot_target_multi-user.ymlplaybook: set_default_boot_target_multi-user.yml
Run the playbook.
[student@workstation system-process]$ansible-playbook \>set_default_boot_target_multi-user.ymlPLAY [Change default runlevel target] ************************************* TASK [Gathering Facts] **************************************************** ok: [servera.lab.example.com] TASK [Default runlevel is multi-user target] ****************************** changed: [servera.lab.example.com] PLAY RECAP **************************************************************** servera.lab.example.com : ok=2 changed=1 unreachable=0 failed=0
Run an ad hoc command to verify that the default boot target is now multi-user.target.
[student@workstation system-process]$ansible webservers -u devops -b \>-a "systemctl get-default"servera.lab.example.com | CHANGED | rc=0 >>multi-user.target