# Day #58: 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

* Write an ansible playbook to create a file on a different server
    
* Write an ansible playbook to create a new user.
    
* Write an ansible playbook to install docker on a group of servers
    

# Task-02

* Write a blog about writing ansible playbooks with the best practices.
    

## **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.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683575543419/55eada4c-5f4d-4c3e-8f52-1d640a3537bd.png?auto=compress,format&format=webp align="left")
        
    2. We can display the ansible playbook as below.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683575708161/b4e87b99-99bc-4d0f-8c2c-18dc8d966e0e.png?auto=compress,format&format=webp align="left")
        
* Write an Ansible playbook to create a new user.
    
    1. Create the ansible-playbook file to create a user in both the node servers.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683575880256/a52ab545-38bb-478e-9785-559a4c89bde8.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683575900116/71b73939-a143-49a4-b73e-37b622829cf8.png?auto=compress,format&format=webp align="left")
        
    2. Run the ansible-playbook file to create the user.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683575945077/e497433a-94ea-49d0-8d63-fafbc5750d77.png?auto=compress,format&format=webp align="left")
        
    3. Let's check in both servers if the users are created.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683576135949/aec8fb0c-7e02-4179-a6fc-a215436959c1.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683576178765/0949b7b7-12d7-438a-927c-dcf6f6bc253d.png?auto=compress,format&format=webp align="left")
        
* 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.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683576244398/8e25ca02-376b-48ec-93f5-9f89a5d804ae.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683576286747/85fdb700-8ca9-4311-af5e-5366c93a2861.png?auto=compress,format&format=webp align="left")
        
    2. Run the playbook file in the master server and we can view the output status.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683576355841/e7cc0756-c4c6-42fa-b700-fb6ff210e832.png?auto=compress,format&format=webp align="left")
        
    3. Let's check the node servers to check the service status of docker.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683576408371/55f7ff47-dec0-4e27-9313-214b21be53aa.png?auto=compress,format&format=webp align="left")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683576433338/e8f530e2-0db8-4543-991a-aebfe4d340a7.png?auto=compress,format&format=webp align="left")
        

# 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:
        
        **COPY**
        
        ```plaintext
         ---
         - name: Install Apache web server
           hosts: webserver
           become: true
           tasks:
             - name: Install Apache2 package
               apt:
                 name: apache2
                 state: latest
             - name: Copy index.html file
               copy:
                 src: /path/to/index.html
                 dest: /var/www/html/index.html
             - name: Start Apache2 service
               service:
                 name: apache2
                 state: started
        ```
        
        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:-
        
        **COPY**
        
        ```plaintext
         PLAY [Install Apache web server] ************************************************
        
         TASK [Gathering Facts] *********************************************************
         ok: [webserver]
        
         TASK [Install Apache2 package] *************************************************
         changed: [webserver]
        
         TASK [Copy index.html file] ****************************************************
         changed: [webserver]
        
         TASK [Start Apache2 service] ***************************************************
         changed: [webserver]
        
         PLAY RECAP *********************************************************************
         webserver                  : ok=4    changed=3    unreachable=0    failed=0
        ```
        
        This output shows that the playbook ran successfully on the target host ("webserver") and that three tasks were changed (i.e., executed successfully).
