Automating Disk Partitioning, Formatting, and Mounting with Ansible – Part 3

Automating Disk Partitioning, Formatting, and Mounting with Ansible – Part 3

Ansible is a powerful automation tool that can simplify the management of server configurations. In this article, we will walk through an Ansible playbook that partitions, formats, and mounts disks, updates the hosts file, and installs Java.

Playbook Overview

The playbook is divided into multiple sections, each performing a specific task. We’ll start with creating partitions, formatting them, and then mounting them to specific directories. Following this, we will update the hosts file and ensure Java is installed.

Step-by-Step Instructions

Creating Partitions

We will use the parted module to create partitions on two disks: /dev/vdb and /dev/vdc.

- hosts: all
  tasks:
    - name: create partitions for vdb and vdc
      parted:
        device: "{{ item }}"
        number: 1
        state: present
      loop:
        - /dev/vdb
        - /dev/vdc        

Explanation:

  • device: "{{ item }}": Specifies the device to partition.
  • number: 1: Denotes the partition number.
  • state: present: Ensures the partition is created.

Formatting Partitions

Next, we will format these partitions with the ext4 filesystem using the filesystem module.

- hosts: all
  tasks:
    - name: format vdb and vdc
      filesystem:
        fstype: ext4
        dev: "{{ item }}"
      loop:
        - /dev/vdb
        - /dev/vdc        

Explanation:

  • fstype: ext4: Specifies the filesystem type.
  • dev: "{{ item }}": Refers to the device to be formatted.

Mounting Partitions

We will mount the formatted partitions to /data1 and /data2 directories using the mount module.

- hosts: all
  tasks:
    - name: mount vdb to /data1 and vdc to /data2
      mount:
        fstype: ext4
        src: "{{ item.dev }}"
        path: "{{ item.path }}"
        state: mounted
      loop:
        - { dev: '/dev/vdb', path: '/data1' }
        - { dev: '/dev/vdc', path: '/data2' }        

Explanation:

  • src: "{{ item.dev }}": Source device to be mounted.
  • path: "{{ item.path }}": Directory where the device will be mounted.
  • state: mounted: Ensures the device is mounted.

Updating Hosts File and Installing Java

After setting up the disks, we will update the /etc/hosts file and install Java using yum.

Copying Hosts File

- hosts: all
  tasks:
    - name: copy hosts files
      synchronize:
        src: "{{ base_dir }}/files/etc/hosts"
        dest: /etc        

Explanation:

  • src: "{{ base_dir }}/files/etc/hosts": Source file to copy.
  • dest: /etc: Destination directory on the target machines.

Installing Java

- hosts: all
  tasks:
    - name: ensure jdk 1.8
      yum:
        name: java-1.8.0-openjdk-devel
        state: present        

Explanation:

  • name: java-1.8.0-openjdk-devel: Java package to install.
  • state: present: Ensures the package is installed.

Running the Playbook

You can run the playbook with the following command:

ansible-playbook kafka.yml -i hosts --extra-vars "base_dir=/vagrant/master/ansible" --tags common --become        

Zookeeper Configuration (zoo.cfg)

Below is a sample configuration for Zookeeper (zoo.cfg):

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/data1/zookeeper
clientPort=2181
maxClientCnxns=1000

server.1=192.168.33.20:2888:3888
server.2=192.168.33.30:2888:3888
server.3=192.168.33.40:2888:3888        


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

Ali Imran的更多文章

社区洞察

其他会员也浏览了