2-Day1-2-Ad-hocCommands
Lab Exercise: Test Ansible Setup with Ad-Hoc Commands
In this step, participants will execute various ad-hoc commands to validate their Ansible setup and perform common tasks. These exercises are divided into core exercises and optional exercises for those who complete the basics early can go for the optional exercises.
Core Exercises (Mandatory for All Participants)
Exercise 1: Ping Managed Nodes
Use the
pingmodule to verify connectivity to all managed nodes.ansible all -m pingExpected Output: A green success message confirming that the managed nodes are reachable.
Exercise 2: Check Host Uptime
Use the
commandmodule to execute theuptimecommand on all nodes.ansible all -m command -a "uptime"Expected Output: Uptime details of each managed node.
Exercise 3: Create a User on Managed Nodes
Use the
usermodule to create a new user namedansible_userXX.ansible all -m user -a "name=ansible_userXX state=present"Expected Output: A new user
ansible_userXXis created on all managed nodes.
Exercise 4: Verify the User avilability on Nodes
Use the
usermodule to create a new user namedansible_userXX.Expected Output: The new user
ansible_userXXshould be present on all managed nodes.
Exercise 5: Install a Package (e.g., tree)
Use the
yumoraptmodule to install thetreepackage.
If it is already installed, please use "state=absent" then execute the above command angain observe the diffrence.
Expected Output: The
treepackage is installed on all managed nodes.
Exercise 6: Start a Service
Use the
servicemodule to start thehttpdservice (or install it first if not present).Expected Output: The
httpdservice is installed, started, and enabled on all managed nodes.
**Optional Exercises **
Exercise 7: Gather System Facts
Use the
setupmodule to gather facts about managed nodes and display the hostname.Expected Output: The hostname of each managed node.
Exercise 8: Copy Files to Managed Nodes
Use the
copymodule to copy a file from the control node to/tmp/on managed nodes.Create a file on the control node:
Copy the file:
Verify on managed nodes:
Exercise 9: Execute a Shell Script on Managed Nodes
Create a simple shell script on the control node:
Use the
scriptmodule to execute the script on all nodes:Expected Output: The hostname of each managed node is displayed.
Exercise 10: Manage Files on Managed Nodes
Use the
filemodule to create a directory/tmp/ansible_test:Use the
filemodule to delete the directory:
Exercise 11: Modify File Contents
Use the
lineinfilemodule to add a line to/etc/motdon managed nodes:Expected Output: The line "Managed by Ansible" is added to the
/etc/motdfile.
Exercise 12: Retrieve Files from Managed Nodes
Use the
fetchmodule to retrieve a file (e.g.,/var/log/messages) from managed nodes to the control node.Expected Output: Files are copied to the specified location on the control node.
Summary of Commands
Exercise
Command/Module
Purpose
Ping
ping
Test connectivity to managed nodes
Uptime
command
Execute shell commands
Create User
user
Manage system users
Install Package
yum/apt
Install packages
Start Service
service
Manage system services
Gather Facts
setup
Collect system information
Copy Files
copy
Copy files to managed nodes
Run Scripts
script
Execute scripts on managed nodes
Manage Directories
file
Create or delete directories
Modify Files
lineinfile
Modify file contents
Retrieve Files
fetch
Copy files from managed nodes to control node
Outcome of This
Participants will understand how to use Ansible ad-hoc commands effectively.
They will gain confidence performing tasks like file management, service handling, and system information gathering.
Fast learners can attempt optional exercises to deepen their knowledge and explore advanced modules.
Last updated