Day 57: Ansible Hands-on with video

Day 57: Ansible Hands-on with video

Ansible is fun, you saw in last few days how easy it is. Today we will write a blog explanation for the [ansible video](https://youtu.be/SGB7EdiP39E).

What is Ansible

Ansible is an automation platform that provides a simple yet powerful way to automate IT tasks. It is agentless, which means you don’t need to install any software on the remote servers you want to manage. Instead, Ansible uses SSH (Secure Shell) to communicate with target servers and execute tasks. This approach not only simplifies setup but also enhances security.

Ansible’s key features include:

  • Declarative Configuration Management: Ansible allows you to define the desired state of your infrastructure in simple, human-readable YAML files.

  • Task Automation: You can automate tasks such as software installation, configuration changes, and system updates.

  • Idempotence: Ansible ensures that running a task multiple times has the same result as running it once, making it safe and predictable.

  • Extensibility: It’s easy to extend Ansible’s functionality using custom modules and plugins.

Ansible Setup with Servers

Setting up Ansible is a straightforward process:

  1. Install Ansible: You can install Ansible on your local machine or a dedicated control server. Use package managers like apt, yum, or pip for installation.

  2. Inventory File: Create an inventory file that lists the IP addresses or hostnames of the servers you want to manage. This file is essential for defining your target hosts.

  3. SSH Configuration: Ensure that SSH access is set up on the target servers. Ansible uses SSH to connect to these servers securely.

  4. SSH Keys: To avoid password prompts during execution, set up SSH key-based authentication between your control machine and target servers.

  5. Testing Connection: Use the ansible command to test the connection to your servers, ensuring everything is set up correctly.

SSH Tutorial

SSH (Secure Shell) is a critical component of Ansible. It provides a secure method for connecting to remote servers. Here’s a brief tutorial:

  • SSH Keys: Generate an SSH key pair on your control machine using ssh-keygen. Then, copy the public key to your target servers' ~/.ssh/authorized_keys file to enable passwordless authentication.

  • SSH Config: Customize your SSH client configuration in the ~/.ssh/config file to streamline connections. Set options like the default user, identity file, and host aliases.

  • SSH Agent: Use ssh-agent to manage your SSH keys securely. Add your private key to the agent using ssh-add, eliminating the need to enter your passphrase repeatedly.

  • SSH Port Forwarding: Learn about SSH port forwarding to acess services on remote servers securely.

Creating Ansible Inventory

An Ansible inventory is a file that specifies the hosts and groups of hosts you want to manage. Here’s how to create one:

  • Inventory File Format: Ansible inventory files are typically in INI or YAML format. You can choose the format that suits your needs.

  • Hosts and Groups: Define individual hosts and group them logically based on their roles or functions.

  • Variables: You can assign variables to hosts or groups, allowing you to customize configurations and tasks.

  • Dynamic Inventories: Ansible supports dynamic inventories generated by scripts or external sources, making it easy to manage large and dynamic environments

Ansible Commands

Once you’ve set up Ansible and created your inventory, you’re ready to start automating tasks. Here are some essential Ansible commands:

  • Ad-Hoc Commands: Use ansible commands to run ad-hoc tasks on your servers. For example, ansible all -m ping will ping all hosts in your inventory to check connectivity.

  • Playbooks: Create YAML playbooks that define the desired state of your infrastructure and the tasks to achieve it. Execute playbooks using the ansible-playbook command.

  • Roles: Organize your playbooks and tasks into reusable roles for better code organization and modularity.

  • Variables and Templates: Utilize variables to customize tasks and templates to generate configuration files dynamically.

  • Handlers: Define handlers to trigger specific actions when a change occurs, such as restarting services after a configuration update.