1、playbook简介
Playbook与ad-hoc相比,是一种完全不同的运用ansible的方式,类似与saltstack的state状态文 件。ad-hoc无法持久使用,playbook可以持久使用。
playbook是由一个或多个play组成的列表,play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓的task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联合起来按事先编排的机制完成某一任务
2、playbook核心元素
Hosts 执行的远程主机列表
Tasks 任务集
Varniables 内置变量或自定义变量在playbook中调用
Templates 模板,即使用模板语法的文件,比如配置文件等
Handlers 和notity结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行
tags 标签,指定某条任务执行,用于选择运行playbook中的部分代码。
3、ansible语法
playbook使用yaml语法格式,后缀可以是yaml,也可以是yml。
缩进:YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用tabs。
冒号:以冒号结尾的除外,其他所有冒号后面所有必须有空格。
短横线:表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一列表。
4、playbook初识
[root@ansible ~]# cat playbook01.yml
--- #固定格式
- hosts: 192.168.1.31 #定义需要执行主机
remote_user: root #远程用户
vars: #定义变量
http_port: playtest #变量
tasks: #定义一个任务的开始
- name: create new file #定义任务的名称
file: name=/tmp/{{ playtest }} state=touch #调用模块,具体要做的事情
- name: create new user
user: name=test02 system=yes shell=/sbin/nologin
执行playbook
[root@ansible ~]# ansible-playbook playbook01.yml
##第一次执行也可以先使用-C检测语法
[root@ansible ~]# ansible-playbook -C playbook01.yml
5、playbook相关参数
[root@ansible PlayBook]# ansible-playbook -h
#ansible-playbook常用选项:
--check or -C #只检测可能会发生的改变,但不真正执行操作
--list-hosts #列出运行任务的主机
--list-tags #列出playbook文件中定义所有的tags
--list-tasks #列出playbook文件中定义的所以任务集
--limit #主机列表 只针对主机列表中的某个主机或者某个组执行
-f #指定并发数,默认为5个
-t #指定tags运行,运行某一个或者多个tags。(前提playbook中有定义tags)
-v #显示过程 -vv -vvv更详细
6、Playbook执行结果返回
红色: 表示有task执行失败或者提醒的信息
黄色:表示执行了且改变了远程主机状态
绿色:表示执行成功
7、playbook变量注册- register
[root@manager ~]# cat f5.yml
---
- hosts: all
tasks:
- name:
shell: netstat -lntp
register: System_Status
- name: Get System Status
debug: msg={{System_Status.stdout_lines}}
8、playbook条件语局-when
playbook中的条件判断语句使用when
[root@manager ~]# cat f6.yml
- hosts: all
remote_user: root
tasks:
- name: Create File
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")
#系统为centos的主机才会执行
- name: Centos Install httpd
yum: name=httpd state=present
when: (ansible_distribution == "CentOS")
#系统为ubuntu的主机才会执行
- name: Ubuntu Install httpd
yum: name=httpd2 state=present
when: (ansible_distribution == "Ubuntu")
9、playbook循环语句
1、标准循环使用场景-批量安装软件
[root@manager ~]# cat f7.yml
---
- hosts: all
remote_user: root
tasks:
- name: Installed Pkg
yum: name={{ item }} state=present
with_items:
- wget
- tree
- lrzsz
9.1、playbook循环使用场景-批量创建用户
[root@manager ~]# cat f7.yml
- hosts: all
remote_user: root
tasks:
- name: Add Users user: name={{ item.name }} groups={{ item.groups }} state=present
with_items:
- { name: 'testuser1', groups: 'bin' }
- { name: 'testuser2', groups: 'root' }
9.2、playbook循环使用场景-批量拷贝
[root@manager ~]# cat f7.yml
- hosts: all
remote_user: root
tasks:
- name: Configure Rsync Server
copy: src={{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }} with_items:
- {src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644"}
- {src: "rsync.passwd", dest: "rsync.passwd", mode: "0600"}
10、play异常处理
默认Playbook会检查命令和模块的返回状态,如遇到错误就中断playbook的执行 加入参数: ignore_errors: yes 忽略错误
[root@manager ~]# cat f9.yml
---
- hosts: all
remote_user: root
tasks:
- name: Ignore False
command: /bin/false
ignore_errors: yes
- name: touch new file
file: path=/tmp/bgx_ignore state=touch
11、playbook tags标签
对一个对象打一个标签
对一个对象打多个标签
对多个对象打一个标签 '
标签使用,通过tags和任务对象进行捆绑,控制部分或者指定的task执行
-t: 执行指定的tag标签任务
--skip-tags: 执行--skip-tags之外的标签任务
演示:
[root@manager ~]# cat f10.yml
---
- hosts: all
remote_user: root
tasks:
- name: Install Nfs Server
yum: name=nfs-utils state=present
tags:
- install_nfs
- install_nfs-server
- name: Service Nfs Server
service: name=nfs-server state=started enabled=yes
tags: start_nfs-server
指定tags执行:
使用-t指定tags执行, 多个tags使用逗号隔开即可
[root@manager ~]# ansible-playbook -t install_nfs-server f10.yml
排除指定tags执行:
[root@manager ~]# ansible-playbook --skip-tags install_nfs-server f10.yml
12、playbook Handlers
[root@m01 ~]# cat webserver.yml
- hosts: web
remote_user: root
#1.定义变量,在配置文件中调用
vars: http_port: 8881
#2.安装httpd服务
tasks:
- name: Install Httpd Server
yum: name=httpd state=present
#3.使用template模板,引用上面vars定义的变量至配置文件中
- name: Configure Httpd Server
template: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
#4.启动Httpd服务
- name: Start Httpd Server
service: name=httpd state=started enabled=yes
#5.检查Httpd服务当前的运行的端口状态
- name: Get Httpd Server Port
shell: netstat -lntp|grep httpd
register: Httpd_Port
#6.输出Httpd运行的状态至面板
- name: Out Httpd Server Status
debug: msg={{ Httpd_Port.stdout_lines }}
ignore_errors: yes
#7.如果配置文件发生变化会调用该handlers下面的模块
handlers:
- name: Restart Httpd Server
service: name=httpd state=restarted
13、playbook Include
include用来动态的包含tasks任务列表,include_tasks新版/include老版。
include调用任务方式 #主入口文件
[root@mha ~]# cat main.yml
- hosts: all
remote_user: root
tasks:
- include_tasks: f20.yml
- include_tasks: f21.yml
#f20.yml
[root@mha ~]# cat f20.yml
- name: create file1
command: touch file
#21.yml
[root@mha ~]# cat f21.yml
- name: create file2
command: touch file2
有问题请加博主微信进行沟通!
全部评论