このステップでは、Ansible を使用して systemd サービスを管理し、cron ジョブをスケジュールし、システムブートターゲットを設定する方法を学びます。これは、サービス可用性の維持と、インフラストラクチャ全体での定型タスクの自動化に不可欠です。
-
サービスとプロセス管理用の新しいプロジェクトディレクトリに移動します。
cd ~/project
mkdir system-process
cd system-process
-
この演習用のインベントリファイルを作成します。
cat << EOF > inventory.ini
[webservers]
localhost ansible_connection=local
EOF
-
Apache HTTP サーバーサービスを管理するプレイブックを作成します。 これは基本的なサービス管理を示します。
nano service_management.yml
以下のコンテンツを追加します。
---
- name: Manage Apache HTTP Server Service
hosts: webservers
become: true
tasks:
- name: Start and enable httpd service
ansible.builtin.service:
name: httpd
state: started
enabled: yes
- name: Create a simple index.html
ansible.builtin.copy:
content: |
<html>
<head><title>Ansible Managed Server</title></head>
<body>
<h1>Welcome to Ansible Managed Apache Server</h1>
<p>This server is configured and managed by Ansible.</p>
<p>Service started at: $(date)</p>
</body>
</html>
dest: /var/www/html/index.html
owner: apache
group: apache
mode: "0644"
- name: Verify httpd service is running
ansible.builtin.service_facts:
- name: Display httpd service status
ansible.builtin.debug:
var: ansible_facts.services['httpd.service']
-
cron ジョブスケジューリング用のプレイブックを作成します。 これは自動化されたタスクスケジューリングを示します。
nano create_crontab_file.yml
以下のコンテンツを追加します。
---
- name: Schedule recurring cron jobs
hosts: webservers
become: true
tasks:
- name: Create labex user for cron jobs
ansible.builtin.user:
name: labex
state: present
create_home: yes
- name: Schedule system monitoring cron job
ansible.builtin.cron:
name: System monitoring log
job: "date >> /home/labex/system_monitor.log && df -h >> /home/labex/system_monitor.log"
minute: "*/5"
hour: "*"
user: labex
cron_file: system-monitoring
state: present
- name: Schedule daily log rotation
ansible.builtin.cron:
name: Daily log cleanup
job: "find /home/labex -name '*.log' -mtime +7 -delete"
minute: "0"
hour: "2"
weekday: "*"
user: labex
cron_file: log-cleanup
state: present
- name: Schedule weekly system update check
ansible.builtin.cron:
name: Weekly update check
job: "dnf check-update > /home/labex/update_check.log 2>&1"
minute: "0"
hour: "3"
weekday: "0"
user: labex
cron_file: update-check
state: present
-
at を使用した単発タスクスケジューリング用のプレイブックを作成します。
nano schedule_at_task.yml
以下のコンテンツを追加します。
---
- name: Schedule one-time tasks with at
hosts: webservers
become: true
become_user: labex
tasks:
- name: Schedule immediate system info collection
ansible.posix.at:
command: "uname -a > ~/system_info_$(date +%Y%m%d_%H%M%S).txt"
count: 2
units: minutes
unique: yes
state: present
- name: Schedule delayed service status check
ansible.posix.at:
command: "systemctl status httpd > ~/httpd_status_$(date +%Y%m%d_%H%M%S).txt"
count: 5
units: minutes
unique: yes
state: present
-
システムブートターゲットを管理するプレイブックを作成します。
nano boot_target_management.yml
以下のコンテンツを追加します。
---
- name: Manage system boot targets
hosts: webservers
become: true
tasks:
- name: Check current default target
ansible.builtin.command:
cmd: systemctl get-default
register: current_target
changed_when: false
- name: Display current boot target
ansible.builtin.debug:
msg: "Current default boot target: {{ current_target.stdout }}"
- name: Set default boot target to multi-user
ansible.builtin.systemd:
name: multi-user.target
enabled: yes
when: current_target.stdout != "multi-user.target"
- name: Verify the boot target change
ansible.builtin.command:
cmd: systemctl get-default
register: new_target
changed_when: false
- name: Display new boot target
ansible.builtin.debug:
msg: "New default boot target: {{ new_target.stdout }}"
-
サービス管理プレイブックを実行します。
ansible-playbook -i inventory.ini service_management.yml
これにより、httpd サービスが開始され、ウェルカムページが作成されます。
-
cron ジョブスケジューリングプレイブックを実行します。
ansible-playbook -i inventory.ini create_crontab_file.yml
これにより、システム監視とメンテナンスのためのいくつかのスケジュール済みタスクが作成されます。
-
単発タスクスケジューリングプレイブックを実行します。
ansible-playbook -i inventory.ini schedule_at_task.yml
これにより、at コマンドを使用した即時タスクがスケジュールされます。
-
ブートターゲット管理プレイブックを実行します。
ansible-playbook -i inventory.ini boot_target_management.yml
これにより、システムのデフォルトブートターゲットがチェックされ、必要に応じて変更されます。
-
スケジュールされたタスクとサービスを確認します。
## Check cron jobs
sudo cat /etc/cron.d/system-monitoring
sudo cat /etc/cron.d/log-cleanup
sudo cat /etc/cron.d/update-check
## Check at jobs
sudo atq
## Check httpd service status
sudo systemctl status httpd
## Test the web server
curl localhost
## Check system monitoring log (wait a few minutes for cron to run)
sudo cat /home/labex/system_monitor.log
-
必要に応じてスケジュールされたタスクを削除するためのクリーンアッププレイブックを作成します。
nano remove_scheduled_tasks.yml
以下のコンテンツを追加します。
---
- name: Remove scheduled tasks
hosts: webservers
become: true
tasks:
- name: Remove system monitoring cron job
ansible.builtin.cron:
name: System monitoring log
user: labex
cron_file: system-monitoring
state: absent
- name: Remove log cleanup cron job
ansible.builtin.cron:
name: Daily log cleanup
user: labex
cron_file: log-cleanup
state: absent
- name: Remove update check cron job
ansible.builtin.cron:
name: Weekly update check
user: labex
cron_file: update-check
state: absent
Ansible を使用してサービス管理、タスクスケジューリング、およびシステム設定を正常に自動化し、インフラストラクチャの維持と監視を自動化するための基盤を提供しました。