Ansible Playbook

1. Install Ansible

Ensure Ansible is installed on your control node (the machine from which you run the playbooks).

sudo apt update
sudo apt install ansible -y        

2. Set Up Inventory File

Define the inventory file (hosts), which lists the managed nodes and groups of nodes. The default inventory file is located at /etc/ansible/hosts, but you can specify a custom file using the -i option.

Example (hosts file):

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com        

3. Create a Playbook File

A playbook is a YAML file containing one or more plays. Each play maps a group of hosts to roles.

Example (site.yml):

---
- name: Install and configure web servers
  hosts: webservers
  become: yes
  tasks:
    - name: Ensure Apache is installed
      apt:
        name: apache2
        state: present
        update_cache: yes

    - name: Ensure Apache is started
      service:
        name: apache2
        state: started

    - name: Deploy HTML file
      copy:
        src: /path/to/index.html
        dest: /var/www/html/index.html

- name: Install and configure database servers
  hosts: dbservers
  become: yes
  tasks:
    - name: Ensure MySQL is installed
      apt:
        name: mysql-server
        state: present
        update_cache: yes

    - name: Ensure MySQL is started
      service:
        name: mysql
        state: started        

4. Playbook Structure

  • Hosts: The target group of hosts.
  • Become: Elevate privilege to run the tasks as a superuser.
  • Tasks: Define the list of tasks to execute.

5. Run the Playbook

Execute the playbook using the ansible-playbook command.

ansible-playbook -i hosts site.yml        

Thank you for reading

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

Manasee Suuba的更多文章

  • How Pods Differ from Containers

    How Pods Differ from Containers

    Containers are the basic building blocks for running individual applications, pods provide the orchestration-friendly…

  • How would you perform a rolling update of an application across multiple servers to ensure zero downtime?

    How would you perform a rolling update of an application across multiple servers to ensure zero downtime?

    Use the serial keyword to update servers in batches. Ensure that a load balancer routes traffic away from the servers…

    1 条评论
  • AWS IAM (Identity and Access Management)

    AWS IAM (Identity and Access Management)

    AWS IAM is a service that enables you to manage access to AWS resources securely. It controls who (users, roles, or…

  • AWS Instance

    AWS Instance

    In AWS (Amazon Web Services), an instance refers to a virtual server that runs applications on the AWS infrastructure…

  • VPC

    VPC

    A VPC (Virtual Private Cloud) in AWS is a logically isolated section of the AWS cloud where you can launch AWS…

  • IAM

    IAM

    AWS IAM (Identity and Access Management) is a web service that helps you securely control access to AWS services and…

  • S3 bucket

    S3 bucket

    Amazon S3 (Simple Storage Service) is a scalable and durable object storage service provided by AWS. S3 Buckets are…

  • Cloudfront

    Cloudfront

    Amazon CloudFront is a content delivery network (CDN) service provided by AWS. It helps deliver content (web pages…

  • Cloudwatch

    Cloudwatch

    Amazon CloudWatch is a monitoring and observability service provided by AWS that helps you track the performance and…

  • CIDR Block

    CIDR Block

    A CIDR block (Classless Inter-Domain Routing block) is a range of IP addresses that is defined by a base IP address and…

社区洞察

其他会员也浏览了