Ansible is a powerful automation tool that simplifies the management and configuration of servers. One of its core features is the ability to run playbooks, which are YAML files containing tasks to be executed on remote hosts. In this article, we'll walk through the process of creating and running a simple Ansible playbook that performs basic tasks on a specified host.
Before we begin, ensure you have the following:
1. Ansible Installed: Ansible must be installed on your control node. You can install it using pip:
pip install ansible
2. SSH Access: Ensure you have SSH access to the remote host(s) you will manage with Ansible.
3. Sudo Privileges: The user account used must have sudo privileges if you plan to run tasks that require elevated permissions.
First, let's create a new YAML file that will contain our playbook. Open a terminal and use the nano text editor to create and edit the file.
nano example1.yaml
In the example1.yaml file, write the following content:
---
- name: "demo play"
hosts: 10.0.0.100
become: true
tasks:
- name: "create a file"
file:
path: /usr/abc.txt
state: touch
- name: "restart a service"
systemd:
name: crond
state: restarted
Let's break down this playbook:
file module to create a file.
After saving the file and exiting the text editor, run the playbook using the ansible-playbook command:
ansible-playbook example1.yaml
This command will execute the tasks defined in example1.yaml on the specified host. Ansible will connect to the host, perform the file creation, and restart the service.
Congratulations! You have successfully created and run a simple Ansible playbook. This playbook demonstrates basic tasks such as creating a file and restarting a service. Ansible's powerful features and simplicity make it an essential tool for IT automation. As you become more familiar with Ansible, you can create more complex playbooks to automate various administrative tasks and streamline your workflows.
Happy automating!