一、什么是Playbook

playbook可以理解为ansible的shell脚本,它是一个编排工具,作用是使用编排出能够重复利用的ansible脚本,并并发处理多台服务器。

二、playbook使用事件

1.服务器初始化

(1)playbook的task任务

#本脚本用来进行Centos7系统初始化,请谨慎使用

########Yum Tools########
- name: Update yum repo
copy: src={{ item }} dest=/etc/yum.repos.d/
with_fileglob:
- yum/CentOS-Base.repo
- yum/docker-ce.repo - name: Basic lib install
yum: name={{ item }} state=latest update_cache=yes
with_items:
- epel-release
- libselinux-python
- glibc
- gcc
- make
- cmake
- zlib
- python-pip - name: Basic tools install
yum: name={{ item }} state=latest update_cache=yes
with_items:
- zip
- net-tools
- lrzsz
- htop
- axel
- wget
- curl
- telnet
- iotop
- vim
- dmidecode
- sysstat
- ntp
- net-snmp
- rsync ########Selinux Firewalld Disable########
- name: Selinux dsiable
lineinfile:
dest: /etc/selinux/config
regexp: '^SELINUX='
line: 'SELINUX=disabled' - name: Selinux stop
selinux: state=disabled - name: Firewalld disable
service: name=firewalld state=stopped enabled=no ########Ulimit Init########
- name: Ulimit change
shell: ulimit -SHn 102400 - name: Ulimit change rc.local
lineinfile:
dest: /etc/rc.local
regexp: 'ulimit -SHn 102400'
backrefs: no
line: 'ulimit -SHn 102400' - name: Change limits.conf soft
lineinfile:
dest: /etc/security/limits.conf
regexp: '\* soft nofile [0-9]+'
backrefs: no
line: '* soft nofile 102400' - name: Change limits.conf hard
lineinfile:
dest: /etc/security/limits.conf
regexp: '\* hard nofile [0-9]+'
backrefs: no
line: '* hard nofile 102400' - name: Change system.conf DefaultLimitCORE
lineinfile:
dest: /etc/systemd/system.conf
regexp: 'DefaultLimitCORE'
backrefs: no
line: 'DefaultLimitCORE=infinity' - name: Change system.conf DefaultLimitNOFILE
lineinfile:
dest: /etc/systemd/system.conf
regexp: 'DefaultLimitNOFILE'
backrefs: no
line: 'DefaultLimitNOFILE=100000' - name: Change system.conf
lineinfile:
dest: /etc/systemd/system.conf
regexp: 'DefaultLimitNPROC'
backrefs: no
line: 'DefaultLimitNPROC=100000' ########Change Hostname########
- hostname : name={{ hostname }} - name: Add hosts
lineinfile:
dest: /etc/hosts
line: '{{ ansible_eth0.ipv4.address }} {{ hostname }}' ########Disk Init########
#- name: New Disk Partition
# script: scripts/disk.sh "{{ disk }}" #执行 disk.sh 参数{{ disk }} 对应xfs.yml的disk: /dev/vdb #磁盘名字
# become: yes
# become_method: sudo #- name: New Disk Format(xfs)
# filesystem: fstype=xfs dev="{{ partition }}" opts="-fn ftype=1" #格式化磁盘分区
# become: yes
# become_method: sudo #- name: New Disk Mount
# mount: name="{{ mountDir }}" src="{{ partition }}" fstype=xfs state=mounted #挂在目录
# become: yes
# become_method: sudo ########Create Directory########
- name: Create Directory
file: path={{ item }} state=directory
with_items:
- /opt/hxapps
- /opt/hxwww
- /opt/hxlog/
- /opt/hxscripts
- /opt/hxupload
- /opt/hxbackup ########Docker install########
- name: Install docker
yum: name=docker-ce state=present
async: 0
poll: 10 - name: config docker Storage type and location
lineinfile:
dest: /usr/lib/systemd/system/docker.service
regexp: '^ExecStart='
line: 'ExecStart=/usr/bin/dockerd --graph=/opt/docker' - service: name=docker enabled=yes state=started - name: Install docker-compose
shell: pip install docker-compose
async: 0
poll: 10 ########Ssh Init#######
- name: Open ssh PubkeyAuthentication
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '#PubkeyAuthentication yes'
backrefs: yes
line: 'PubkeyAuthentication yes' - name: Open ssh AuthorizedKeysFile
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '#AuthorizedKeysFile'
backrefs: yes
line: 'AuthorizedKeysFile' - name: Close ssh PasswordAuthentication
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '^PasswordAuthentication yes'
backrefs: yes
line: 'PasswordAuthentication no' - name: Change ssh port
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '#Port 22'
backrefs: yes
line: 'Port 8022' - name: Echo /etc/ssh/sshd_config
shell: egrep "Port|AuthorizedKeysFile|PubkeyAuthentication|PasswordAuthentication" /etc/ssh/sshd_config - name: Create .ssh
file: path=/root/.ssh owner=root group=root mode=700 state=directory - name: Add keys
copy: src=public_key/authorized_keys dest=/root/.ssh/authorized_keys owner=root group=root mode=600 - name: Restart sshd
service: name=sshd state=restarted enabled=yes

(2)引用的disk.sh

#!/bin/bash

DISK=$

CHECK_EXIST=`/sbin/fdisk -l > /dev/null | grep -o "$DISK"`
[ ! "$CHECK_EXIST" ] && { echo "Error: Disk is not found !"; exit ;} echo "" > /tmp/disk.log CHECK_DISK_EXIST=`/sbin/fdisk -l > /dev/null | grep -o "$DISK[1-9]"`
[ ! "$CHECK_DISK_EXIST" ] || { echo "WARNING: ${CHECK_DISK_EXIST} is Partition already !"; exit ;} echo "" > /tmp/disk.log /sbin/fdisk /dev/sdb<<EOF
d
n
p t w
EOF

(3)执行的sysinit.yml

- hosts: sysinit
vars:
disk: /dev/vdb
partition: /dev/vdb1
mountDir: /opt
roles:
- sysinit

(4)inventory文件

########Init hosts list########
#[groups:children]
#group
#[groups:vars]
#ansible_ssh_port=
#ansible_user=root [sysinit:vars]
ansible_user=root #远程用户
ansible_port= #远程端口
ansible_ssh_pass=dingkai. #远程密码 [sysinit]
#服务器IP hostname=服务器主机名

Ansible-playbook服务器初始化的更多相关文章

  1. Ansible playbook 批量修改服务器密码 先普通后root用户

    fsckzy   Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...

  2. ansible roles实践——服务器初始化

    1.服务器初始化可以做哪些工作 关闭selinux ntp同步时间 修改dns为自建dns 配置ssh互信 修改yum源 设置主机名 内核参数优化 安装jdk 2.roles编写

  3. Ansible:roles初始化系统

    简介 本文介绍ansible的roles,通过roles来实现系统的初始化,其相当于将ansible的playbook拆分.本文通过Jenkins,传参,调用playbook来初始化系统. Githu ...

  4. ansible-playbook编写服务器初始化脚本

    1.原理:通过limit的参数,限制新定义的服务器.即可给新买的服务器初始化优化.(如下图所示) 首先我们编写一个总入口的palybook脚本: init.yml --- - hosts: all u ...

  5. ansible playbook批量改ssh配置文件,远程用户Permission denied

    最近手里的数百台服务器需要改/etc/ssh/sshd_config的参数,禁止root直接登陆,也就是说 [root@t0 ~]# cat /etc/ssh/sshd_config | grep R ...

  6. ansible笔记(10):初识ansible playbook

    ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...

  7. 写Ansible playbook添加zabbix被监控的对象

    本主题达到的效果是能通过编写Ansible Playbook,创建zabbix主机组,把被监控的对象加入到zabbix监控系统中,同时链接到对象的模板. 1.准备工作 在zabbix服务器上面,我们需 ...

  8. ansible playbook模式及语法

    一.什么是playbook及其组成 什么是playbook playbook 翻译过来就是"剧本" playbook的组成 play:定义的是主机的角色 task:定义的是具体执行 ...

  9. ansible - playbook(剧组)

    目录 ansible - playbook(剧组) 常用命令 五种传参方式 常用元素详解 tags handlers template when 循环 嵌套循环 ansible - playbook( ...

随机推荐

  1. 聚合函数 Aggregate Function

    聚合函数是用来统计每个分组的统计信息,它们要跟 group by 一起使用,用来将每个分组所有数据 聚合 成一条统计数据. 包括 max/min/count/avg/sum 等. -- 按照部门进行分 ...

  2. Shiro(一)

    1 权限管理 1.1 什么是权限管理? 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问权限的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自 ...

  3. ArrayList为什么是线程不安全的

    首先需要了解什么是线程安全:线程安全就是说多线程访问同一代码(对象.变量等),不会产生不确定的结果. 既然说ArrayList是线程不安全的,那么在多线程中操作一个ArrayList对象,则会出现不确 ...

  4. IP地址(参考百度百科)

    题目1:用子网掩码划分网络时,如果划分8个子网,则需要4位.因为24-2>=8.(全0代表网络自身,全1代表广播地址,所以减2) 以C类地址为例,(1)8个子网,则4位子网,主机号为后4位,主机 ...

  5. electron监听系统托盘,electron是否最小化到系统托盘

    在项目中需要判断窗口是否最小化在系统托盘上,任务栏那已经关闭,查了一晚上的api,始终找不到可以调用的方法,最后绞尽脑汁想到了一个办法,那就是在点右上角的关闭按钮时,加个全局变量,用来标识已经最小到系 ...

  6. javaweb上传大文件的问题

    总结一下大文件分片上传和断点续传的问题.因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况.http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件 ...

  7. [CF780C]Andryusha and Colored Balloons 题解

    前言 完了,完了,咕值要没了,赶紧写题解QAQ. 题意简述 给相邻的三个节点颜色不能相同的树染色所需的最小颜色数. 题解 这道题目很显然可以用深搜. 考虑题目的限制,如果当前搜索到的点为u, 显然u的 ...

  8. CTSC&APIO 2017游记

    Day 0 早上4点多起床赶飞机,起床的时候发现闹钟调成下午4点的了...(虽然说早就已经被父母的洗漱声音吵醒了) 飞机上碎觉.到了北京发现比福州还热...而且北京今天意外地好天气,没有传言中的&qu ...

  9. tf_upgrade_v2.exe实验

    实验前 import tensorflow as tf import numpy as np #create data x_data=np.random.rand(100).astype(np.flo ...

  10. 关于Struts2_2.3.24中FilterDispatcher过期的问题

    今天在使用最新版Struts2.3.24时,总是报出如下警告: ******************************************************************** ...