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. kindeditor使用记录

    --------------------------资源 百度下载包  kindeditor-4.1.11-zh-CN 解压后根据需要选择asp / asp.net / jsp / php 文件夹之一 ...

  2. [转]Groovy One Liners to Impress Your Friends

    Link:http://arturoherrero.com/2011/06/04/10-groovy-one-liners-to-impress-your-friends/ I find that c ...

  3. 【转】JavaScript里Function函数实现可变参数

    转载:  http://www.oschina.net/question/54100_15938 使用javascript类库函数时,经常会遇到一个函数,可以使用不同个数的参数的情况 比如:exp(v ...

  4. BAPC 2014:Button Bashing(暴力+bfs)

    题意: 给出n,m,代表微波炉有n个按钮,要求达到总时间为m 然后给出n个数,代表n个按钮能增加的时间,问最少几步,能够使得按出的总时间大于等于要求的时间,并且相差最小 输出最小的步数与相差的最小值 ...

  5. mysql+gtid主从同步

    安装mysql  yum install mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-common-5.7.17-1. ...

  6. json转译的问题

    今天遇到一个之前没遇到的情况 这边调用接口的时候,一串json数据我直接解析成php的时候,太长导致我在使用 $json = json_encode($list);转译成的时候,里面有一个数据是时间戳 ...

  7. rpm命令相关

    ### .列出所有安装过的包 rpm -qa | grep sql ### .如何获得某个软件包的文件全名. rpm -q mysql ### .rpm包中的文件安装到那里 rpm -ql lrzsz ...

  8. thinkphp Model的使用

    4.1 放在哪儿?项目/模块/Model目录下以本教程为例,Home模块的Model/Home/Model/目录下 4.2 model类文件叫什么?模型名: DemoModel.class.php 4 ...

  9. tail -f 实时跟踪一个日志文件的输出内容

    tail -f  实时跟踪一个日志文件的输出内容 http://hittyt.iteye.com/blog/1927026 https://blog.csdn.net/mengxianhua/arti ...

  10. .NET控制台程序监听程序退出

    There are mainly 2 types of Win32 applications, console application and window application. They hav ...