Experiment 7
Experiment 7
1. Introduction to Ansible
What Is Ansible?
Ansible is an open-source IT automation and configuration management tool. It allows you to
manage multiple servers and perform tasks such as:
Configuration Management: Automate the configuration of servers.
Application Deployment: Deploy applications consistently.
Orchestration: Coordinate complex IT workflows and processes.
Key Concepts in Ansible
Inventory:
An inventory is a file (usually in INI or YAML format) that lists the hosts (or groups of
hosts) you want to manage. It tells Ansible which machines to target.
Playbook:
A playbook is a YAML file that defines a set of tasks to be executed on your target hosts.
It is the heart of Ansible automation. In a playbook, you specify:
o Hosts: The target machines (or groups) on which the tasks should run.
o Tasks: A list of actions (using modules) that should be executed.
o Modules: Reusable, standalone scripts that perform specific actions (e.g.,
installing packages, copying files, configuring services).
Modules:
Ansible comes with a large collection of built-in modules (such as apt, yum, copy,
service, etc.). These modules perform specific tasks on target hosts. You can also
write custom modules
Agentless: Ansible uses SSH to communicate with target hosts, so no agent needs to
be installed on them.
Simplicity: Playbooks use simple YAML syntax, making them easy to write and
understand.
Idempotence: Ansible tasks are idempotent, meaning running the same playbook
multiple times yields the same result, ensuring consistency.
Scalability: Ansible can manage a small number of servers to large infrastructures
with hundreds or thousands of nodes.
The Ansible control node is a system used to connect to and manage Ansible host servers.
Proceed with the steps below to set up the control node on the main
server:
1. Create an administrator-level user for the control node. Use the adduser
command:
Optionally, provide more details about the user by answering questions. Press
Enter to skip a question.
3. Use the following usermod command to assign superuser privileges to the
account:
sudo usermod -aG sudo username
A membership in the sudo group allows the user to utilize the sudo command to
perform administrative tasks.
4. Switch to the newly created user on the control node:
sudo su username
Note: The Ansible control node can be a dedicated server, a local machine, or a virtual
machine running Ubuntu.
Note: If an SSH key pair with the same name already exists, SSH displays a warning asking
the user to decide whether to overwrite it. Overwriting makes the previous SSH key pair
unusable, so ensure the old keys are no longer needed before confirming.
Ansible hosts are remote servers managed by the Ansible control node. Each
host must have the control node's SSH public key into authorized_keys directory.
Apply the steps below for each new Ansible host:
1. Use the following ssh-copy-id command on the control node to copy the public
key to a host:
ssh-copy-id username@remote-host
Replace [username] with an existing administrative user on the host system and
[remote-host] with the remote host domain or IP address. For example, to copy
the key to the user ansible on the host with the local IP address 192.168.0.81,
type:
2. Type yes and hit Enter when asked whether to continue connecting to an
authenticated host.
3. Enter the remote host account password.
The utility uploads the public key to the remote host account.
Use the APT package manager to install the Ansible package on the control node
system:
1. Ensure the package index is up to date
sudo apt update
2. Install Ansible on Ubuntu with the following command:
sudo apt install ansible –y
Check that Ansible was successfully installed on your Ubuntu system using the
ansible command:
ansible --version
The output displays the Ansible version number, the location of the configuration
file, the path to the executable, and other information.
Follow the steps below to create an inventory file on the control node:
1. Create the ansible subdirectory in the etc directory:
sudo mkdir -p /etc/ansible
The Ansible control node is now set up to control the connected remote hosts.
STEP 8: Create a sample playbook called HelloWorld.yml and type the following