DevOps(Day-58): Ansible Playbooks

DevOps(Day-58): Ansible Playbooks

TABLE OF CONTENTS

Ansible Playbooks

Ansible playbooks run multiple tasks, assign roles, and define configurations, deployment steps, and variables. If you’re using multiple servers, Ansible playbooks organize the steps between the assembled machines or servers and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.

Task-01: Creating a user and installing Docker

  • Write an Ansible playbook to create a file on a different server

    1. Connect the node server to the master server by writing the host file.

    2. We can display the ansible playbook as below.

  • Write an Ansible playbook to create a new user.

    1. Create the ansible-playbook file to create a user in both the node servers.

    2. Run the ansible-playbook file to create the user.

    3. Let's check in both servers if the users are created.

  • Write an Ansible playbook to install docker on a group of servers

    1. Create an ansible-playbook file for installing docker in the node servers.

    2. Run the playbook file in the master server and we can view the output status.

    3. Let's check the node servers to check the service status of docker.

Task-02: Explaining Ansible Playbook

  • Write a blog about writing Ansible playbooks with the best practices.

    An Ansible playbook is a configuration management tool that allows you to automate IT infrastructure tasks, such as deploying applications, configuring servers, and managing network devices. It is written in YAML format and consists of a series of tasks that are executed on one or more hosts.

    The structure of an Ansible playbook consists of several sections:

    1. Hosts: This section defines the target hosts that the playbook will run on. This can be a single host or a group of hosts. You can specify hosts by name, IP address, or through a dynamic inventory.

    2. Variables: This section defines the variables that will be used in the playbook. Variables can be defined at different levels of scope, such as at the playbook level, the host level, or the task level. They can be used to store data that will be reused throughout the playbook.

    3. Tasks: This section defines the tasks that will be executed on the target hosts. A task is a single action that Ansible will perform, such as installing a package, copying a file, or running a command. Each task can have one or more actions that are performed in order.

    4. Handlers: Handlers are similar to tasks, but they are only executed when notified by a task. They are typically used for actions that require a restart or reload of a service, such as restarting a web server after a configuration change.

    5. Roles: Roles are a way to organize your playbook into logical units. A role is a collection of tasks, variables, files, and templates that can be reused across multiple playbooks.

      Here's an example of a simple Ansible playbook that installs the Apache web server on a single host:

In this example, the name field is a descriptive name for the playbook. The hosts field specifies that the playbook will run on a host named "webserver". The become field indicates that Ansible will run the tasks as a superuser.

The tasks section defines the tasks that will be executed on the target host. In this example, the tasks install the Apache2 package, copy an index.html file to the web server's root directory, and start the Apache2 service.

Overall, Ansible playbooks are a powerful way to automate infrastructure management tasks, and they can help reduce errors, increase efficiency, and streamline workflows.

The output will be:-

  1. This output shows that the playbook ran successfully on the target host ("webserver") and that three tasks were changed (i.e., executed successfully). The output also includes a summary of the results of each task, as well as a play recap at the end.

Thanks for reading my article. Have a nice day.

WRITTEN BY Biswaraj Sahoo --AWS Community Builder | DevOps Engineer | Docker | Linux | Jenkins | AWS | Git | Terraform | Docker | kubernetes

Empowering communities via open source and education. Connect with me over linktree: linktr.ee/biswaraj333