Ansible

*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,
Here's a simple Ansible playbook with two tasks: one task to install Docker and another task to install Kubernetes components (Kubectl and Minikube):
---
- name: Simple Playbook for Docker and Kubernetes
hosts: target_hosts
become: true
tasks:
- name: Install Docker
apt:
name: docker.io
state: present
- name: Install Kubernetes components
apt:
name:
- kubectl
- minikube
state: present
In this playbook:
The play is named "Simple Playbook for Docker and Kubernetes" and targets the hosts specified in the
target_hostsgroup.The
become: truestatement allows the playbook to run with escalated privileges, typically usingsudo.The playbook consists of two tasks:
The first task uses the
aptmodule to install Docker using the packagedocker.ioon the target hosts.The second task uses the
aptmodule to install Kubernetes components, includingkubectlandminikube.
To execute this playbook, make sure you have an inventory file (inventory.ini) that lists the target hosts under the [target_hosts] group.
Run the playbook using the following command in the terminal:
ansible-playbook -i inventory.ini playbook.yml
This playbook will install Docker and Kubernetes components (kubectl and Minikube) on the target hosts.
Keep in mind that the playbook assumes you are targeting Debian/Ubuntu-based systems. If your target hosts are using a different package manager or operating system, you may need to adjust the playbook accordingly.
As always, please ensure that you are running this playbook on test or development environments first, and thoroughly understand the implications of installing Docker and Kubernetes components on your production systems before proceeding.
Here's another simple Ansible playbook to install Nginx, this time using the apt module for package management on Debian/Ubuntu systems and including a task to start the Nginx service:
---
- name: Install and Start Nginx
hosts: webservers
become: true
tasks:
- name: Install Nginx package
apt:
name: nginx
state: present
- name: Start Nginx service
service:
name: nginx
state: started
enabled: yes
In this playbook:
The play is named "Install and Start Nginx" and targets the hosts in the
webserversgroup.The
become: truestatement allows the playbook to run with escalated privileges.Two tasks are defined:
The first task uses the
aptmodule to install the Nginx package, specifying the package name (nginx) and the desired state (present).The second task uses the
servicemodule to start the Nginx service, ensuring it is running and enabled to start at boot (enabled: yes).
Make sure you have an inventory file (e.g., inventory.ini) that lists the target hosts under the [webservers] group.
Run the playbook using the following command:
ansible-playbook -i inventory.ini playbook.yml
This playbook will install Nginx on the target hosts defined in the inventory using the apt package manager and start the Nginx service.
Feel free to modify the playbook to suit your specific needs, such as adjusting package names, package managers, or adding further configuration tasks for Nginx.
In Ansible, you can use the ansible command-line tool to perform various tasks, such as running playbooks, executing ad-hoc commands, gathering facts, and managing inventories. Here are some common ansible command examples:
Ad-hoc Command:
Run a simple command on all hosts:
ansible all -m command -a "ls -l"Install a package on all hosts (Debian/Ubuntu):
ansible all -m apt -a "name=package_name state=present"Install a package on all hosts (Red Hat/CentOS):
ansible all -m yum -a "name=package_name state=present"
Running a Playbook:
Execute a playbook named
playbook.ymlon thetarget_hostsgroup:ansible-playbook -i inventory.ini playbook.yml
Gathering Facts:
Collect system information from all hosts:
ansible all -m setup
Managing Inventory:
Check the connectivity of all hosts defined in the inventory file:
ansible all -m ping
Using Variables:
Set variables dynamically using the
-eflag:ansible-playbook -i inventory.ini playbook.yml -e "variable_name=value"
Filtering Hosts:
Run a command on hosts that match a specific pattern:
ansible 'web*' -m command -a "ls -l"
Specifying the User:
Run a command with a specific user:
ansible all -m command -a "ls -l" -u username
Using Privilege Escalation:
Run a command with privilege escalation (sudo):
ansible all -m command -a "ls -l" -b
Checking Playbook Syntax:
Validate the syntax of a playbook:
ansible-playbook playbook.yml --syntax-check
These are just a few examples of how you can use the ansible command-line tool. The command syntax and options may vary based on your specific use case, inventory configuration, and requirements. You can find more information and options in the official Ansible documentation.
In a daily use Ansible playbook, you might utilize various modules to perform routine tasks. Below are some common Ansible modules that you may include in your daily playbook:
apt/yum: Used for package management on Debian/Ubuntu and Red Hat-based systems, respectively. You can install, update, and remove packages using these modules.
service: Used to manage services on remote hosts. You can start, stop, restart, and enable/disable services with this module.
file: Used to manage files and directories on remote hosts. You can create, delete, or change file permissions using this module.
copy: Used to copy files from the control node to the remote hosts. This is useful for transferring configuration files or scripts.
template: Similar to the
copymodule but allows you to use Jinja2 templates to customize files before copying them to the remote hosts.command/shell: Used to run commands or shell scripts on remote hosts. The
commandmodule is preferred for simple tasks, whileshellis more suitable for complex shell commands.cron: Used to manage cron jobs on remote hosts. You can add or remove cron jobs using this module.
user: Used to manage user accounts on remote hosts. You can create, delete, or modify users using this module.
group: Used to manage user groups on remote hosts. You can create, delete, or modify groups using this module.
lineinfile: Used to modify lines in a file on remote hosts. It's helpful for editing configuration files.
get_url: Used to download files from the internet and place them on the remote hosts.
git: Used to manage Git repositories. You can clone, pull, or checkout specific branches with this module.
pip: Used for managing Python packages on remote hosts. You can install, upgrade, or uninstall Python packages with this module.
systemd: Used for managing systemd services on modern Linux distributions. You can enable, disable, start, stop, or restart services.
These are just a few examples of the many modules available in Ansible. Depending on your specific use case and tasks, you might explore other modules as well. Ansible's extensive library of modules enables you to automate various tasks and configurations in your daily DevOps operations.




