playbook 相当于多个命令的编排组合然后一起运行,类似写脚本。在学习 playbook 之前需要了解 yaml 格式。

编写playbook的步骤:

  • 定义主机与用户
  • 编写任务列表
  • 执行 playbook

当然 playbook 支持拆分多个文件,并且可以使用多种维度的封装,例如定义变量、任务、处理程序等,鼓励代码/文件复用。

下面是安装 nginx 和 ntp server 的示例。

1、文件目录结构

[root@localhost ansible_demo]# tree nginx_ntp/
nginx_ntp/
├── group_vars
│   ├── all
│   └── webserver
├── hosts
├── roles
│   ├── common
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   │   └── ntp.conf.j2
│   │   └── vars
│   │   └── main.yml
│   └── web
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   └── templates
│   └── nginx2.conf
└── site.yml 11 directories, 11 files

2、Inventory 文件

[root@localhost nginx_ntp]# cat hosts
[webserver]
192.168.34.129
192.168.34.130

3、playbook 入口文件

[root@localhost nginx_ntp]# cat site.yml
---
- name: apply common configuration to all nodes
hosts: all
roles:
- common - name: configure and deploy the webserver and application code
hosts: webserver
roles:
- web

4、组变量文件

根据 inventory 来区分,all 表示所有,webserver 则指 hosts 中的 webserver section

[root@localhost nginx_ntp]# cd group_vars/
[root@localhost group_vars]# ll
total 8
-rw-r--r--. 1 root root 32 Mar 6 18:12 all
-rw-r--r--. 1 root root 72 Mar 6 18:24 webserver
[root@localhost group_vars]# cat all
---
ntpserver: ntp.sjtu.edu.cn [root@localhost group_vars]# cat webserver
---
worker_processes: 4
root: /data1
worker_connections: 1024
user: www

5、roles 设置

5.1、角色 common 配置

安装 ntp server,配置文件并同步时间启动 ntp server

  • 任务入口 tasks
[root@localhost nginx_ntp]# cd roles/common/tasks/
[root@localhost tasks]# cat main.yml
---
- name: install ntp server
yum: name=ntp state=present - name: set zone info
shell: \cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime - name: update time
shell: ntpdate asia.pool.ntp.org - name: configure ntp file
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntp - name: start ntp server
service: name=ntpd state=started enabled=true - name: test to see if selinux is running
command: getenforce
register: sestatus
changed_when: false
  • name 为 configure ntp file 的任务运行后的 callback handlers
[root@localhost common]# cat handlers/main.yml
---
- name: restart ntp
service: name=ntpd state=restarted
  • template 源文件
[root@localhost common]# cat templates/ntp.conf.j2
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 :: 1 server {{ ntpserver }}
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
  • role common 内置变量
[root@localhost common]# cat vars/main.yml
---
ntpserver: 210.72.145.44

5.2 角色 web 配置

安装 nginx,配置文件并启动

  • 任务入口 tasks
[root@localhost web]# cat tasks/main.yml
--- - name: install nginx
yum: name=nginx state=latest - name: add user
shell: useradd {{ user }} - name: write config
template: src=nginx2.conf dest=/etc/nginx/nginx.conf
notify:
- restart nginx - name: ensure nginx is running
shell: /usr/sbin/nginx -c /etc/nginx/nginx.conf
  • name 为 write config 的任务运行后的 callback handlers
[root@localhost web]# cat handlers/main.yml
---
- name: restart nginx
service: name=nginx state=restarted
  • template 文件
[root@localhost web]# cat templates/nginx2.conf
user www;
worker_processes {{ worker_processes }}; events {
worker_connections {{ worker_connections }};
} http {
include mime.types;
default_type application/octet-stream; sendfile on;
keepalive_timeout 65; server {
listen 80;
server_name localhost;
root {{ root }}; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

6 检查语法和运行

[root@localhost nginx_ntp]# ansible-playbook -i hosts site.yml --syntax-check
ERROR! Problem parsing file '/data1/ansible_demo/nginx_ntp/group_vars/all': line 2, column 1

发现 all 文件出错,修改后继续

[root@localhost nginx_ntp]# ansible-playbook -i hosts site.yml --syntax-check

playbook: site.yml
[root@localhost nginx_ntp]# ansible-playbook -i hosts site.yml -f 10 PLAY [apply common configuration to all nodes] ********************************************************************************************** TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.34.130]
ok: [192.168.34.129] TASK [common : install ntp server] **********************************************************************************************************
ok: [192.168.34.129]
ok: [192.168.34.130] TASK [common : set zone info] ***************************************************************************************************************
changed: [192.168.34.130]
changed: [192.168.34.129] TASK [common : update time] *****************************************************************************************************************
changed: [192.168.34.130]
changed: [192.168.34.129] TASK [common : configure ntp file] **********************************************************************************************************
changed: [192.168.34.129]
changed: [192.168.34.130] TASK [common : start ntp server] ************************************************************************************************************
changed: [192.168.34.130]
changed: [192.168.34.129] TASK [common : test to see if selinux is running] *******************************************************************************************
ok: [192.168.34.130]
ok: [192.168.34.129] RUNNING HANDLER [common : restart ntp] ******************************************************************************************************
changed: [192.168.34.129]
changed: [192.168.34.130] PLAY [configure and deploy the webserver and application code] ****************************************************************************** TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.34.130]
ok: [192.168.34.129] TASK [web : install nginx] ******************************************************************************************************************
ok: [192.168.34.130]
ok: [192.168.34.129] TASK [web : add user] ***********************************************************************************************************************
changed: [192.168.34.129]
changed: [192.168.34.130] TASK [web : write config] *******************************************************************************************************************
changed: [192.168.34.129]
changed: [192.168.34.130] TASK [web : ensure nginx is running] ********************************************************************************************************
changed: [192.168.34.129]
changed: [192.168.34.130] RUNNING HANDLER [web : restart nginx] *******************************************************************************************************
changed: [192.168.34.129]
changed: [192.168.34.130] PLAY RECAP **********************************************************************************************************************************
192.168.34.129 : ok=14 changed=9 unreachable=0 failed=0
192.168.34.130 : ok=14 changed=9 unreachable=0 failed=0 [root@localhost nginx_ntp]#

 7、被控机验证

[root@localhost yum.repos.d]# ps -ef | grep ntp
ntp : ? :: ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
root : pts/ :: grep ntp
[root@localhost yum.repos.d]# ps -ef | grep nginx
root : ? :: nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www : ? :: nginx: worker process
www : ? :: nginx: worker process
www : ? :: nginx: worker process
www : ? :: nginx: worker process
root : pts/ :: grep nginx

Ansible 笔记 (3) - 编写 playbook的更多相关文章

  1. ansible笔记(11):初识ansible playbook(二)

    ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...

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

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

  3. ansible笔记(8):初识ansible playbook

    回顾总结:我们来想象一个工作场景,看看怎样把之前的知识点应用到这个工作场景中.假设,我们想要在192.168.10.2主机上安装nginx并启动,我们可以在ansible控制主机中执行如下3条命令. ...

  4. ansible笔记(12):handlers的用法

    ansible笔记():handlers的用法 这篇文章会介绍playbook中handlers的用法. 在开始介绍之前,我们先来描述一个工作场景: 当我们修改了某些程序的配置文件以后,有可能需要重启 ...

  5. ansible笔记(1)在centos中安装ansible

    ansible笔记():ansible的基本概念 一些基础概念 ansible是什么? 它是一个"配置管理工具",它是一个"自动化运维工具",如果你没有使用过任 ...

  6. Hadoop学习笔记(5) ——编写HelloWorld(2)

    Hadoop学习笔记(5) ——编写HelloWorld(2) 前面我们写了一个Hadoop程序,并让它跑起来了.但想想不对啊,Hadoop不是有两块功能么,DFS和MapReduce.没错,上一节我 ...

  7. ansible笔记(7):常用模块之系统类模块

    ansible笔记():常用模块之系统类模块 cron模块 cron模块可以帮助我们管理远程主机中的计划任务,功能相当于crontab命令. 在了解cron模块的参数之前,先写出一些计划任务的示例,示 ...

  8. ansible笔记(8):常用模块之系统类模块(二)

    ansible笔记():常用模块之系统类模块(二) user模块 user模块可以帮助我们管理远程主机上的用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 此处我们介绍一些user模块 ...

  9. ansible笔记(9):常用模块之包管理模块

    ansible笔记():常用模块之包管理模块 yum_repository模块 yum_repository模块可以帮助我们管理远程主机上的yum仓库. 此处我们介绍一些yum_repository模 ...

随机推荐

  1. linux 中特殊符号用法详解

    # 井号 (comments)#管理员  $普通用户 脚本中 #!/bin/bash   #!/bin/sh井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注解文字,不会被执行 ...

  2. 发布MVC项目到服务器上时候遇到的 模块 DirectoryListingModule 通知 ExecuteRequestHandler 处理程序 StaticFile 错误代码 0x00000000

    应用程序“HMW121197”中的服务器错误错误摘要HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容. 详细错误信息模块 DirectoryListingM ...

  3. kegg富集分析之:KEGGREST包(9大功能)

    这个包依赖极有可能是这个:https://www.kegg.jp/kegg/docs/keggapi.html ,如果可以看懂会很好理解 由于KEGG数据库分享数据的策略改变,因此KEGG.db包不在 ...

  4. JDA 8.0.0.0小版本升级

    一.升级前关服务和进行备份 二.开始升级 三. 开以下四个服务 1237 四个服务开启后需重新执行SSIS中的startingFP(去掉backupdata 05 importFP) 当以下值为0,代 ...

  5. redis安装和命令使用

    前言: redis是一个key-value的存储系统,value支持string.list.set.zset.hash五种类型,且支持数据的本地存储   一.安装redis 前提:linux下需要安装 ...

  6. input上传图片

    1.通过input自身的onchange事件触发: <input id="file" type="file" accept="image/*&q ...

  7. myeclipse 10激活,本人已测试过可行

    激活步骤: 下载myeclipse 10硬解程序包: ed2k://|file|%5Bmyeclipse.10.0.%E6%9B%B4%E6%96%B0%E5%8F%91%E5%B8%83%28%E7 ...

  8. Windows phone Toast消息推送 学习笔记

    简单介绍: Windows phone平台支持三种形式的推送通知: 1.Tile——也就是在Start屏幕程序平铺图标 2.Toast——创建一个显示在当前屏幕中的Toast弹出窗口 3.Raw——有 ...

  9. ECMAScript6新特性之Reflect

    一 Reflect.ownKeys()获取对象属性. 可枚举的.不可枚举的.自有的.继承的. let fruit = { '2' : 'mango', [Symbol.for('pink')] : ' ...

  10. discuz回贴通知插件实现-插件后台管理配置

    1.登出discuz后台,再次设计插件 2.使用变量