Ansible Level 02 Day 02: Ansible Install Package¶
This document outlines the solution for Ansible Level 02 Day 02. The objective was to configure an inventory of application servers and automate the installation of the zip package across all of them using Ansible's yum module.
Table of Contents¶
- Ansible Level 02 Day 02: Ansible Install Package
- Table of Contents
- Task Overview
- Step-by-Step Solution
- Deep Dive: Ansible Concepts Used
Task Overview¶
Objective: Install the zip package on all application servers using Ansible.
- Inventory Location:
/home/thor/playbook/inventory - Playbook Location:
/home/thor/playbook/playbook.yml - Target Nodes: All app servers (
stapp01,stapp02,stapp03) - Module:
yum - Execution Constraint: Must run successfully via
ansible-playbook -i inventory playbook.ymlwithout any extra CLI arguments.
Step-by-Step Solution¶
1. Create the Inventory File¶
We must configure the inventory to include all three application servers and their connection credentials to satisfy the strict execution command constraint.
Command:
mkdir -p /home/thor/playbook
cd /home/thor/playbook
vi inventory
Content:
[app]
stapp01 ansible_host=stapp01 ansible_user=tony ansible_ssh_pass=Ir0nM@n
stapp02 ansible_host=stapp02 ansible_user=steve ansible_ssh_pass=Am3ric@
stapp03 ansible_host=stapp03 ansible_user=banner ansible_ssh_pass=BigGr33n
[all:vars]
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
2. Create the Playbook¶
Create the YAML playbook to define the package installation task. Since installing packages requires root access, we must include become: yes so Ansible runs the command with sudo privileges.
Command:
vi playbook.yml
Content:
---
- name: Install zip package on application servers
hosts: all
become: yes
tasks:
- name: Ensure zip is installed
yum:
name: zip
state: present
3. Execute and Validate¶
Run the playbook to provision the packages.
Command:
ansible-playbook -i inventory playbook.yml
Expected Output:
PLAY [Install zip package on application servers] ***********************************
TASK [Gathering Facts] **************************************************************
ok: [stapp01]
ok: [stapp02]
ok: [stapp03]
TASK [Ensure zip is installed] ******************************************************
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]
PLAY RECAP **************************************************************************
stapp01 : ok=2 changed=1 unreachable=0 failed=0 ...
stapp02 : ok=2 changed=1 unreachable=0 failed=0 ...
stapp03 : ok=2 changed=1 unreachable=0 failed=0 ...
Manual Verification (Optional): You can verify the package was installed by running an ad-hoc command across the inventory:
ansible all -i inventory -a "zip -v"
Deep Dive: Ansible Concepts Used¶
The yum Module¶
Ansible uses the yum module to interact with the Yellowdog Updater Modified package manager on RedHat-based systems (like CentOS used in this lab).
* name: zip: Specifies the package we want to manage.
* state: present: This tells Ansible to ensure the package is installed. If zip is already installed, Ansible will recognize this and do nothing (reporting ok instead of changed). If we wanted the absolute newest version, we would use state: latest.
Privilege Escalation (become: yes)¶
By default, Ansible connects as the user specified in the inventory (e.g., tony, steve). Regular users do not have permissions to install software system-wide via yum. Adding become: yes instructs Ansible to use sudo to elevate its privileges to root before running the task. In this specific environment, these users have passwordless sudo rights, allowing the playbook to run without prompting for a sudo password.