Day 58: Ansible Playbooks

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

1.Write an ansible playbook to create a file on a different server

Create an inventory file for Ansible?

No alt text provided for this image

Create an ansible playbook to create a file on a different server

No alt text provided for this image

In this playbook, we define a single task that uses the file module to create the file. The path parameter specifies the full path to the file we want to create, and state parameter set to touch which will create the file if it doesn't exist and do nothing if it does exist.

You can run this playbook using the ansible-playbook command

ansible-playbook file-name.yml -i <inventory-file-path> --private-key=<private-key-path>        
No alt text provided for this image

Verify that the file has been created at the specified location

No alt text provided for this image

Verify that the file has been created on different servers

ansible all -a "ls /home/ubuntu" -i <inventory-file> --private-key=<key-path>        
No alt text provided for this image


2.Write an ansible playbook to create a new user.

No alt text provided for this image

In this playbook, we define a single task that uses the user module to create the new user.

Run playbook using the ansible-playbook command,

No alt text provided for this image

To check if the user has been created, look for the user's name in the /etc/passwd file."

No alt text provided for this image


3. Write an ansible playbook to install docker on a group of servers

No alt text provided for this image

In this playbook, we define three tasks that install Docker on a group of servers. The hosts parameter specifies the group of servers where Docker will be installed. The become parameter is set to yes, which means that the tasks will use elevated privileges to install Docker.

The first task adds the Docker GPG key to the apt keyring, which is required for package signature verification.

The second task adds the Docker repository to the list of sources used by apt-get. This allows the system to download Docker packages from the Docker repository.

The final task installs Docker using the apt module. We specify the package name as docker-ce, which installs the Community Edition of Docker.


Run this playbook using the ansible-playbook command,

No alt text provided for this image

Verify that Docker has been installed on multiple servers.

ansible all -a "docker --version" -i <inventory-path> --private-key=<key-path>        
No alt text provided for this image


Task-02

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

Ansible is a powerful open-source automation tool that allows you to automate IT infrastructure management. Ansible playbooks are the configuration files that describe the desired state of your systems. Writing Ansible playbooks with best practices can make your automation more robust, reliable, and scalable. In this blog, we will discuss some best practices for writing Ansible playbooks.

Use a clear structure:

Make sure your playbook is organized and easy to read. Use comments and section headers to explain what each part of the playbook does. Avoid nesting too many tasks or plays inside each other, as it can make the playbook difficult to read and maintain.

Use idempotent tasks:

Idempotency is one of the key principles of Ansible. It means that a task should be designed in such a way that it can be executed repeatedly without changing the state of the system. This makes your playbook more robust and prevents unexpected changes to the system. You can achieve idempotency by using modules that are designed to be idempotent, such as apt, yum, and copy.

Example:

- name: Install Apache web server
? apt:
? ? name: apache2
? ? state: latest        

In this example, the apt module will only install the Apache web server if it is not already installed or if there is a newer version available.

Use variables:

Variables can make your playbook more flexible and reusable. They allow you to store values that can be used across multiple tasks and playbooks. You can define variables in the playbook itself or in separate files, such as group_vars or host_vars.

Example:

- name: Install Nginx web server
? apt:
? ? name: nginx
? ? state: latest

- name: Configure Nginx web server
? template:
? ? src: nginx.conf.j2
? ? dest: /etc/nginx/nginx.conf
? vars:
? ? nginx_port: 80        

In this example, we are using a variable nginx_port to define the port number used by Nginx. The variable is defined in the vars section of the template task.

Use roles:

Roles are a way to organize your tasks, variables, and files into reusable components. They can be used across multiple playbooks and provide a way to share functionality between different teams. Roles can also make your playbook more modular and easier to maintain.

Example:

- name: Install web serve
? hosts: all
? roles:
? ? - webserver        

In this example, we are using a role called webserver to install the web server on the all group of hosts.

Use tags:

Tags allow you to selectively run specific tasks or groups of tasks within a playbook. This can be useful when testing or debugging your playbook or when you need to run a subset of tasks. Tags can be added to individual tasks or to entire plays.

Example:

- name: Install web serve
? hosts: all
? become: yes
? tags:
? ? - webserver


? tasks:
? ? - name: Install Apache web server
? ? ? apt:
? ? ? ? name: apache2
? ? ? ? state: latest
? ? ? tags:
? ? ? ? - apache


? ? - name: Install Nginx web server
? ? ? apt:
? ? ? ? name: nginx
? ? ? ? state: latest
? ? ? tags:
? ? ? ? - nginx        

In this example, we are using tags to selectively run the Apache or Nginx installation tasks.


Use conditionals:

Conditionals allow you to execute a task only if a specific condition is met. This can be useful when you need to run a task only on a subset of hosts or when you need to run a task only if a specific variable is defined.

Example:

---
- name: Example playbook with a conditional
? hosts: all
? vars:
? ? myvar: "foo"
? tasks:
? ? - name: Task 1
? ? ? debug:
? ? ? ? msg: "This task will always run"

? ? - name: Task 2
? ? ? debug:
? ? ? ? msg: "This task will run if myvar equals 'foo'"
? ? ? when: myvar == "foo"

        

In this example, the playbook has two tasks. Task 1 will always run because it doesn't have a conditional attached to it. Task 2 will only run if the variable myvar is equal to "foo". If myvar has any other value, Task 2 will be skipped.

By following these best practices, you can write efficient, modular, and maintainable Ansible playbooks that can be easily adapted to different environments and situations.


Thank you for reading!

要查看或添加评论,请登录

Sayali Shewale的更多文章

  • DevOps Project-3

    DevOps Project-3

    Project Description The project involves hosting a static website using an AWS S3 bucket. Amazon S3 is an object…

    7 条评论
  • DevOps Project-2

    DevOps Project-2

    Project Description The project is about automating the deployment process of a web application using Jenkins and its…

    2 条评论
  • Day 80: DevOps Project-1

    Day 80: DevOps Project-1

    Project Description The project aims to automate the building, testing, and deployment process of a web application…

    4 条评论
  • Day 73 - Setup Grafana on AWS EC2 Instance

    Day 73 - Setup Grafana on AWS EC2 Instance

    Task: Setup grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance Open port…

    2 条评论
  • Day 72 - Grafana

    Day 72 - Grafana

    What is Grafana? Grafana is an open-source data visualization and monitoring tool that allows you to query, visualize…

    3 条评论
  • Day 70 - Terraform Modules

    Day 70 - Terraform Modules

    Modules are containers for multiple resources that are used together. A module consists of a collection of .

    2 条评论
  • Day 69 - Meta-Arguments in Terraform

    Day 69 - Meta-Arguments in Terraform

    When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage…

    1 条评论
  • Day 68 - Scaling with Terraform

    Day 68 - Scaling with Terraform

    Understanding Scaling Scaling is the process of adding or removing resources to match the changing demands of your…

  • Day 67: AWS S3 Bucket Creation and Management using terraform

    Day 67: AWS S3 Bucket Creation and Management using terraform

    AWS S3 Bucket Amazon S3 (Simple Storage Service) is an object storage service that offers industry-leading scalability,…

    1 条评论
  • Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Task: 1.Create a VPC (Virtual Private Cloud) with CIDR block 10.

    3 条评论

社区洞察

其他会员也浏览了