Ansible Level 03 Day 03: Ansible Manage Services¶
This document outlines the solution for Ansible Level 03 Day 03. The objective was to create an Ansible playbook that installs the vsftpd (Very Secure FTP Daemon) package on all application servers and ensures the service is started and enabled to run automatically on boot.
Table of Contents¶
- Ansible Level 03 Day 03: Ansible Manage Services
- Table of Contents
- Task Overview
- Step-by-Step Solution
- Deep Dive: Ansible Concepts Used
Task Overview¶
Objective: Install and configure the vsftpd service across all application servers.
Requirements:
1. Playbook Location: /home/thor/ansible/playbook.yml on the jump host.
2. Target Hosts: All application servers.
3. Inventory Location: /home/thor/ansible/inventory (Already exists).
4. Tasks:
* Install the vsftpd package.
* Start the vsftpd service.
* Enable the vsftpd service (to start on system boot).
5. Constraint: The user thor must be able to run it securely using ansible-playbook -i inventory playbook.yml without any additional arguments.
Step-by-Step Solution¶
1. Verify the Environment¶
Before creating the playbook, it's good practice to verify the pre-existing inventory file to ensure the target hosts and their connection variables are properly set up for passwordless/argumentless execution.
Command:
cd /home/thor/ansible
cat inventory
ansible_user and password/SSH key settings).
2. Create the Playbook¶
Create a YAML playbook to handle the package installation and service management. Both actions modify system-level configurations, so they require root privileges (become: yes).
Command:
vi playbook.yml
Content:
---
- name: Install and configure vsftpd on all app servers
hosts: all
become: yes
tasks:
- name: Install vsftpd package
yum:
name: vsftpd
state: present
- name: Start and enable vsftpd service
service:
name: vsftpd
state: started
enabled: yes
3. Execute and Validate¶
Run the playbook from the /home/thor/ansible/ directory.
Execution Command:
ansible-playbook -i inventory playbook.yml
Expected Output:
PLAY [Install and configure vsftpd on all app servers] ******************************
TASK [Gathering Facts] **************************************************************
ok: [stapp01]
ok: [stapp02]
ok: [stapp03]
TASK [Install vsftpd package] *******************************************************
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]
TASK [Start and enable vsftpd service] **********************************************
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]
PLAY RECAP **************************************************************************
stapp01 : ok=3 changed=2 unreachable=0 failed=0 ...
stapp02 : ok=3 changed=2 unreachable=0 failed=0 ...
stapp03 : ok=3 changed=2 unreachable=0 failed=0 ...
Manual Verification (Optional): To independently verify that the service is running across all nodes, use an ad-hoc command:
ansible all -i inventory -m command -a "systemctl status vsftpd" --become
Deep Dive: Ansible Concepts Used¶
Package Management (yum)¶
The yum module is used to manage packages on RedHat-based distributions (like CentOS, which is commonly used in Stratos DC).
* name: vsftpd: Specifies the package we want to manage.
* state: present: Instructs Ansible to ensure the package is installed. If it is already installed, Ansible reports ok and does nothing (maintaining idempotency).
Service Management (service)¶
The service module is used to control system services (supporting both systemd and init systems).
* state: started: Ensures the service is currently running. If it's stopped, Ansible will start it.
* enabled: yes: Equivalent to running systemctl enable vsftpd. This creates the necessary system symlinks so the service automatically starts up if the server reboots.
Why Order Matters:
The order of tasks in the playbook is crucial. You cannot start or enable the vsftpd service before the vsftpd package is installed. The playbook processes tasks sequentially from top to bottom.