系统:centos7及以上
ansible 结构说明
│ hosts # 部署客户端机器 │ xfs.yml # ansible 控制脚本 │ └─roles └─mount-xfs ├─defaults ├─files │ disk.sh #fdisk 脚本 │ ├─handlers ├─meta ├─tasks │ main.yml #ansible 挂载硬盘 脚本 │ ├─templates └─vars
xfs.yml 说明:
- hosts: all user: root vars: disk: /dev/vdb #磁盘名字 partition: /dev/vdb #分区磁盘名字 mountDir: /apps #挂载点 roles: - mount-xfs
disk.sh 内容
#!/bin/bash DISK=$1 CHECK_EXIST=`/sbin/fdisk -l 2> /dev/null | grep -o "$DISK"` [ ! "$CHECK_EXIST" ] && { echo "Error: Disk is not found !"; exit 1;} echo "1" > /tmp/disk.log CHECK_DISK_EXIST=`/sbin/fdisk -l 2> /dev/null | grep -o "$DISK[1-9]"` [ ! "$CHECK_DISK_EXIST" ] || { echo "WARNING: ${CHECK_DISK_EXIST} is Partition already !"; exit 1;} echo "2" > /tmp/disk.log /sbin/fdisk $DISK<<EOF d n p 1 1 t w EOF
main.yml 说明:
- name: Yum Install yum: name="{{ item }}" state=latest #下载ansible 依赖 with_items: - libselinux-python become: yes become_method: su - name: New Disk Partition script: disk.sh "{{ disk }}" # 执行 disk.sh 参数{{ disk }} 对应xfs.yml的disk: /dev/vdb #磁盘名字 become: yes become_method: su - name: New Disk Format(xfs) filesystem: fstype=xfs dev="{{ partition }}" #分区格式并格式化 become: yes become_method: su - name: New Disk Mount # 挂载磁盘并写入/etc/fstab mount: name="{{ mountDir }}" src="{{ partition }}" fstype=xfs state=mounted become: yes become_method: su
其它说明:
默认阿里云挂载: ansible-playbook -i "10.8.16.42," xfs.yml 带参数执行 ansible-playbook -i "10.8.25.146,10.8.25.147,10.8.25.148," xfs.yml -e "disk=/dev/vdc partition=/dev/vdc mountDir=/logs" 如果其他云平台挂载硬盘报错请注释掉一下内容 #- name: New Disk Partition # script: disk.sh "{{ disk }}" # become: yes # become_method: su
原文:http://juestnow.blog.51cto.com/1515305/1950046