對於剛剛拿到拿到的新機器,惟一的可用信息有,IP地址,服務器登錄用戶名和密碼。python
通常狀況,root用戶是不能直接登錄,只能經過普通用戶跳轉。linux
特別是最近工做中遇到的騰訊雲服務器,申請以後硬盤都尚未掛載上去,還須要手動掛載操做,這個是比較費事的。因此這裏採用ansible中的playbook將以上的工做都一併完成bash
如下的playbook功能包括:服務器
分區新掛載硬盤,默認是一個分區所有使用ssh
格式化新掛載分區code
mount新分區orm
建立遠程管理用戶server
下發ssh公鑰,爲之後ansible遠程管理作準備ip
- hosts: "{{ host }}" remote_user: "{{ loginUser }}" vars: work_path: /opt/object/server/ disk: /dev/sdb partition: /dev/sdb1 mountDir: /data vars_prompt: - name: "username" prompt: "Insert User Name:" default: "www" private: no - name: "passwd" prompt: "Insert Password for the user:" default: "123qwe" private: no tasks: - name: Yum Install yum: name="{{ item }}" state=latest with_items: - libselinux-python become: yes become_method: su - name: New Disk Partition script: ./../script/disk.sh "{{ disk }}" become: yes become_method: su - name: New Disk Format(ext4) filesystem: fstype=ext4 dev="{{ partition }}" opts="-cc" become: yes become_method: su - name: New Disk Mount mount: name="{{ mountDir }}" src="{{ partition }}" fstype=ext4 state=mounted become: yes become_method: su - name: Create Remote User user: name="{{ username }}" password="{{ passwd }}" become: yes become_method: su - name: Set up SSH Key authorized_key: user={{ username }} key="{{ lookup('file', '/home/eric/.ssh/id_rsa.pub') }}" become: yes become_method: su
#!/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 /dev/sdb<<EOF d n p 1 1 t 83 w EOF
ansible-playbook server_init.yml -vvv -k --ask-become-pass -e "loginUser=eric" -e "host=192.168.1.101"