Playbook基本概述
Playbook和Ad-hoc的关系
Playbook的语法格式
一、Playbook基本概述
playbook概念
playbook简单的说就是剧本,能够快速的管理和部署多台主机。它是不同于ansible命令行的执行模式,而是通过编写YAML语法格式的文件(通常以“*.yml”结尾),在通过ansible有序执行YAML语法格式的文件,它支持同步和异步方式,从而达到管理和部署多台主机,playbook较为灵活,功能也更加强大。playbook的主要由play和task两个部分组成。play定义的是主机的角色,task定义的是具体执行的任务。
总的来说:playbook由一个或多个play组成,而一个play里面也可以包含多个task任务。
图1
playbook元素
tasks:任务,由模板定义的操作列表
variables: 定义变量
handlers:处理器,当配置文件发生变化时,触发执行此操作。
二、Playbook和Ad-hoc的关系
- playbook是对AD-Hoc的一种编排方式
- playbook可以持久运行,而Ad-Hoc只能临时运行
- playbook适合复杂的任务,而Ad-Hoc适合做快速简单的任务
- playbook能控制任务执行的先后顺序
三、Playbook的语法格式
playbook是由yml语法格式书写,结构清晰,可读性性强。
语法 | 描述 |
---|---|
缩进 | YAML使用固定的缩进风格表示层级结构,每个缩由两个空格组成,不能使用tab键 |
冒号 | 以冒号结尾的除外,其他所有冒号后面必须要有空格 |
短横线 | 表示列表,使用一个短横杠加一个空格,多个项使用同样的缩进级别作为统一列表 |
举例
① 使用playbook编写一个创建文件的yml
[root@manager~/project1]# cat f1.yml
- hosts: webservers
tasks:
- name: Create New File
file: path=/tmp/123.txt state=touch owner=root group=root mode=0600
- name: Create New File2
file:
path: /tmp/456.txt
state: touch
owner: root
group: root
mode: 0666
[root@manager~/project1]# ansible-playbook --syntax -i hosts f1.yml -->检测语法
[root@manager~/project1]# ansible-playbook -i hosts f1.yml --->模拟执行
[root@manager~/project1]# ansible-playbook -i hosts f1.yml --->执行
案例一
172.16.1.31 server
172.16.1.7 client
172.16.1.8 client
#新增nfsservers服务端
[nfsservers]
172.16.1.31
[backupservers]
172.16.1.41
[web:children]
nfsservers
backupservers
[webservers]
172.16.1.7
172.16.1.8
# 推送秘钥
[root@manager~/project1]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
#测试主机能否ping通
[root@manager~/project1]# ansible all -m ping -i hosts
# 编写一个nfs-server的yml
[root@manager~/project1]# cat nfs_servers.yml
- hosts: nfsservers
tasks:
- name: Installed NFS Server
yum:
name: nfs-utils
state: present
- name: Configure NFS Server
copy:
src: ./file/exports.j2
dest: /etc/exports
owner: root
group: root
mode: 0644
backup: yes
- name: Create NFS Group www
group:
name: www
gid: 666
- name: Create NFS User www
user:
name: www
group: www
uid: 666
create_home: no
shell: /sbin/nologin
- name: Create NFS Share Directory
file:
path: /ansible_data
state: directory
owner: www
group: www
mode: 0755
recurse: yes
- name: Systemd NFS Server
systemd:
name: nfs
state: restarted
enabled: yes
# 编写一个nfs-client的yml
[root@manager~/project1]# cat nfs_client.yml
- hosts: webservers
tasks:
- name: Mount NFS Server share directory
mount:
src: 172.16.1.31:/ansible_data
path: /mnt
fstype: nfs
opts: defaults
state: mounted
[root@manager~/project1]# ansible-playbook --syntax -i hosts nfs_server.yml
[root@manager~/project1]# ansible-playbook --syntax -i hosts nfs_client.yml
[root@manager~/project1]# ansible-playbook -i hosts nfs_server.yml
[root@manager~/project1]# ansible-playbook -i hosts nfs_client.yml
案例二 使用ansible安装并配置nginx服务
[root@manager~/project1]# cat nginx.yml
- hosts: webservers
tasks:
- name: Installed Nginx Server
yum:
name: nginx
state: present
- name:
copy:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: 0644
backup: yes
notify: Restart Nginx Server
- name: Systemd nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
root@manager~/project1]# ansible-playbook -i hosts nginx.yml
案例三、使用ansible playbook方式搭建LAP架构
# 使用yum安装httpd、php firewalld等
# 启动httpd、Firewalld服务
# 添加防火墙规则,放行http的流量,并永久生效
# 使用get_url下载http://fj.xuliangwei.com/public/index.php
[root@manager~/project1]# cat hosts
[nfsservers]
172.16.1.31
[backupservers]
172.16.1.41
[web:children] ---->定义嵌套组
nfsservers
backupservers
[webservers]
172.16.1.7
172.16.1.8
[root@manager~/project1]# cat lamp.yml
- hosts: web
tasks:
- name: Install Httpd Server
yum:
name: httpd
state: present
- name: Install PHP Server
yum:
name: php
state: present
- name: Configure Httpd Website
get_url:
url: http://fj.xuliangwei.com/public/index.php
dest: /var/www/html/index.php
mode: 0644
- name: Systemd Httpd Server
systemd:
name: httpd
state: started
- name: Systemd Firewalld Server
systemd:
name: firewalld
state: started
- name: Configure Firewalld Rule
firewalld:
service: http
state: enabled
root@manager~/project1]# ansible-playbook -i hosts lamp.yml
案例四 搭建可道云盘
# 安装 apache+php
# 配置 copy
# 启动 systemd
# 下载代码 wget
[root@manager~/project1]# cat kod.yml
- hosts: web
tasks:
- name: Installed Httpd Server
yum:
name: httpd
state: present
- name: Installed PHP Server
yum:
name: php
state: present
- name: Get kodcloud Code
synchronize:
src: ./file/kod
dest: /var/www/html/kodcloud
- name: Chomod kodcloud
file:
path: /var/www/html/
owner: root
group: root
mode: 0777
recurse: yes
- name: Systemd Httpd Server
systemd:
name: httpd
state: restarted
root@manager~/project1]# ansible-playbook -i hosts kod.yml
案例五 利用nginx+php搭建可道云盘
编写配置文件
[root@manager~/project1/file]# cat kod.oldxu.com.conf.j2
server {
listen 80;
server_name kod.oldxu.com;
root /code/kod;
client_max_body_size 500m;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@manager~/project1]# cat lnp.yml
- hosts: webservers
tasks:
# 1.配置yum源仓库 nginx
- name: Install Nginx repo
yum_repository:
name: nginx
description: nginx repos
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: no
# 2.配置yum源仓库 php
- name: Install PHP repo
yum_repository:
name: webtatic-php
description: php repos
baseurl: http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck: no
# 3.安装nginx和php
- name: Install Nginx and PHP Packages
yum:
name: "{{ packages }}"
vars:
packages:
- nginx
- php71w
- php71w-cli
- php71w-common
- php71w-devel
- php71w-embedded
- php71w-gd
- php71w-mcrypt
- php71w-mbstring
- php71w-pdo
- php71w-xml
- php71w-mysqlnd
- php71w-pecl-memcached
- php71w-pecl-redis
- php71w-pecl-mongodb
- mod_php71w
- php71w-fpm
- php71w-opcache
# 4.创建程序启动的用户身份
- name: Create Group www
group:
name: www
gid: 666
- name: Create User www
user:
name: www
group: www
uid: 666
create_home: no
shell: /sbin/nologin
# 5.管理nginx配置文件
- name:
copy:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx Server
# 6.管理php-fpm配置文件
- name:
copy:
src: ./file/php-www.conf.j2
dest: /etc/php-fpm.d/www.conf
notify: Restart PHP-FPM Server
# 7.添加kodcloud虚拟主机(检测语法)
- name: Add Nginx VirtHost kod.oldxu.com
copy:
src: ./file/kod.oldxu.com.conf.j2
dest: /etc/nginx/conf.d/kod.oldxu.com.conf
notify: Restart Nginx Server
- name: Init Nginx BseEnv
file:
path: /code
state: directory
owner: www
group: www
recurse: yes
- name: Push KodCloud Code
synchronize:
src: ./file/kod
dest: /code/
- name: Chmod Kodcloud
file:
path: /code
owner: www
group: www
mode: 0777
recurse: yes
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
- name: Systemd PHP-FPM Server
systemd:
name: php-fpm
state: started
enabled: yes
# 8.当nginx或php配置文件发生变更才会触发此操作
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
- name: Restart PHP-FPM Server
systemd:
name: php-fpm
state: restarted
root@manager~/project1]# ansible-playbook -i hosts lnp.yml
做域名解析
网友评论