playbook是ansible用于配置部署的语言。使用YAML格式。

示例

---
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted

主机&用户(hosts)

hosts可以指定一个或者多个主机或者主机组,用冒号分隔
---
- hosts: webservers
remote_user: root
task中定义remote_user
---
- hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
remote_user: yourname
使用sudo
---
- hosts: webservers
remote_user: yourname
sudo: yes
task中使用sudo
---
- hosts: webservers
remote_user: yourname
tasks:
- service: name=nginx state=started
sudo: yes
sudo到不同用户
---
- hosts: webservers
remote_user: yourname
sudo: yes
sudo_user: postgres

任务(tasks)

service模块使用key=value参数
tasks:
- name: make sure apache is running
service: name=httpd state=running
command&shell模块不使用key=value参数
tasks:
- name: disable selinux
command: /sbin/setenforce 0
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
命令行过长允许折行
tasks:
- name: Copy ansible inventory file to client
copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts
owner=root group=root mode=0644
使用变量
tasks:
- name: create a virtual host file for {{ vhost }}
template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}

处理(Handlers)

当foo.conf文件内容改变时,触发服务重启
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handlers最好用于重启服务和重启主机
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted

运行playbook

ansible-playbook playbook.yml -f 10

ansible使用3-playbook的更多相关文章

  1. ansible编译httpd playbook示例

    以下是playbook的内容.它的处理流程是: 1.先在本地下载apr,apr-util,httpd共3个.tar.gz文件. 2.解压这3个文件. 3.安装pcre和pcre-devel依赖包. 4 ...

  2. ansible核心模块playbook介绍

    ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...

  3. ansible中的playbook详解

    首先简单说明一下playbook,playbook是什么呢?根本上说playbook和shell脚本没有任何的区别,playbook就像shell一样,也是把一堆的命令组合起来,然后加入对应条件判断等 ...

  4. ansible剧本之playbook操作

    ansible 剧本 yaml介绍: 是一个编程语言 文件后缀名 yaml yml 数据对应格式: 字典: key: value 列表: [] - ansible-playbook命令格式 执行顺序: ...

  5. mage Ansible学习2 Playbook

    一.上集回顾 1.运维: 手动 --> 标准化 --> 工具化 --> 自动化 --> 智能化 2.工具化 OS Install:PXE ,Cobbler:Virutaliza ...

  6. Linux中级之ansible配置(playbook)

    一.playbooks 如果用模块形式一般有幂等性,如果用shell或者command没有幂等性 playbooks相当于是shell脚本,可以把要执行的任务写到文件当中,一次执行,方便调用 task ...

  7. ansible中的playbook脚本的介绍与使用

    playbook的数据结构,遵循yaml 后缀名为yaml或者yml,这两个后缀名没有区别 字典{key:value} 列表[]或者- - alex - wusir - yantao - yuchao ...

  8. Ansible 创建用户 Playbook 脚本

    创建用户,设置wheel组sudo不需要密码,然后将用户添加到wheel组,并将用户的公钥传输到节点上: --- - name: Linux Create User and Upload User P ...

  9. Ansible playbook API 开发 调用测试

    Ansible是Agentless的轻量级批量配置管理工具,由于出现的比较晚(13年)基于Ansible进行开发的相关文档较少,因此,这里通过一些小的实验,结合现有资料以及源码,探索一下Ansible ...

  10. Ansible之playbook

    简介 playbook是一个非常简单的配置管理和多主机部署系统.可作为一个适合部署复杂应用程序的基础.playbook可以定制配置,可以按指定的操作步骤有序执行,支持同步和异步方式.playbook是 ...

随机推荐

  1. Qt 学习之路 2(16):深入 Qt5 信号槽新语法

    Qt 学习之路 2(16):深入 Qt5 信号槽新语法  豆子  2012年9月19日  Qt 学习之路 2  53条评论 在前面的章节(信号槽和自定义信号槽)中,我们详细介绍了有关 Qt 5 的信号 ...

  2. [转]cron语法

    最近在搞whenever时看到可以用cron语法设置定时任务.所以研究了下cron 语法. every '0 0 27-31 * *'do command "echo 'you can us ...

  3. [Groovy] Private fields and methods are not private in groovy

    如题,, 具体介绍请参看: http://refaktor.blogspot.com/2012/07/private-fields-and-methods-are-not.html

  4. hdu4081 秦始皇修路(次小生成树)

    题目ID:hdu4081   秦始皇修路 题目链接:点击打开链接 题目大意:给你若干个坐标,每个坐标表示一个城市,每个城市有若干个人,现在要修路,即建一个生成树,然后有一个魔法师可以免费造路(不消耗人 ...

  5. SHA_1计算消息摘要

    /** * SHA_1计算消息摘要 * @param bytes 待计算数据 * @return */ public static String SHA_1(byte[] bytes) { Strin ...

  6. 非局部均值(Nonlocal-Mean)

    转载自网站:http://www.cnblogs.com/luo-peng/p/4785922.html 非局部均值去噪(NL-means)   非局部均值(NL-means)是近年来提出的一项新型的 ...

  7. STL之set(唯一且有顺序)

    set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据, 在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序.应该注意的是set中数元素的值不能直接被改变. ...

  8. Dev Express Report 学习总结(八)Dev Express Reports 常见问题总结

    1. 在新建ASP.NET Dev Express Report时的两种方式: A. 右键Add DevExpress Item->New Item->All->从Web Repor ...

  9. JavaSE---对象序列化

    1.对象序列化机制 允许把内存中的Java对象转换成平台无关的二进制流,从而可以将二进制流持久保存到磁盘 或 在网络中直接传输: (目的:使得对象可以脱离程序的运行而独立存在) package com ...

  10. EOF是什么

    我学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示"文字流"(stream)的结尾.这里的"文字流",可以是文件(file) ...