ansible-playbook
来源:互联网 发布:dsa数据 编辑:程序博客网 时间:2024/05/20 11:52
ansbile playbook简介
playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制同唱一台大戏。下面是一个简单示例。
实例
- hosts: webnodesvars:http_port: 80max_clients: 256remote_user: roottasks:- name: ensure apache is at the latest versionyum: name=httpd state=latest- name: ensure apache is runningservice: name=httpd state=startedhandlers:- name: restart apacheservice: name=httpd state=restarted
1.1 playbook基础组件
1.1.1 Hosts和Users
playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;remote_user则用于指定远程主机上的执行任务的用户。如下面示例中的
-hosts: webnodesremote_user: root
不过,remote_user也可用于各task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户`。
- hosts: webnodesremote_user: testtasks:- name: test connectionping:remote_user:testsudo: yes
1.1.2 任务列表和action
play的主体部分是task list。task
list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自下而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在更正playbook后重新执行一次即可。
task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。
每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出。
定义task的可以使用“action: module options”或“module:
options”的格式,推荐使用后者以实现向后兼容。如果action一行的内容过多,也中使用在行首使用几个空白字符进行换行。
tasks:- name: make sure apache is runningservice: name=httpd state=running`在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:tasks:- name: disable selinuxcommand: /sbin/setenforce 0如果命令或脚本的退出码不为零,可以使用如下方式替代:tasks:- name: run this command and ignore the resultshell: /usr/bin/somecommand || /bin/true或者使用ignore_errors来忽略错误信息:tasks:- name: run this command and ignore the resultshell: /usr/bin/somecommandignore_errors: True`1.1.3 handlers
用于当关注的资源发生变化时采取一定的操作。
“notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。
- name: template configuration filetemplate: src=template.j2 dest=/etc/foo.confnotify:- restart memcached- restart apachehandler是task列表,这些task与前述的task并没有本质上的不同。handlers:- name: restart memcachedservice: name=memcached state=restarted- name: restart apacheservice: name=apache state=restarted
案例:heartbeat.yaml- hosts: hbhostsremote_user: roottasks:- name: ensure heartbeat latest versionyum: name=heartbeat state=present- name: authkeys configure filecopy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys- name: authkeys mode 600file: path=/etc/ha.d/authkeys mode=600notify:- restart heartbeat- name: ha.cf configure filecopy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cfnotify: - restart heartbeathandlers:- name: restart heartbeatservice: name=heartbeat state=restarted
二、rolesansilbe自1.2版本引入的新特性,用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。一个roles的案例如下所示:site.ymlwebservers.ymldbservers.ymlroles/common/files/templates/tasks/handlers/vars/meta/webservers/files/templates/tasks/handlers/vars/meta/
而在playbook中,可以这样使用roles:
- hosts: webserversroles:- common- webservers
也可以向roles传递参数,例如:
- hosts: webserversroles:- common- { role: foo_app_instance, dir: '/opt/a', port: 5000 }- { role: foo_app_instance, dir: '/opt/b', port: 5001 }
甚至也可以条件式地使用roles,例如:
- hosts: webserversroles:- { role: some_role, when: "ansible_os_family == 'RedHat'" }
2.1 创建role的步骤(1) 创建以roles命名的目录;(2) 在roles目录中分别创建以各角色名称命名的目录,如webservers等;(3) 在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录;用不到的目录可以创建为空目录,也可以不创建;(4) 在playbook文件中,调用各角色;2.2 role内各目录中可用的文件tasks目录:至少应该包含一个名为main.yml的文件,其定义了此角色的任务列表;此文件可以使用include包含其它的位于此目录中的task文件;files目录:存放由copy或script等模块调用的文件;templates目录:template模块会自动在此目录中寻找Jinja2模板文件;handlers目录:此目录中应当包含一个main.yml文件,用于定义此角色用到的各handler;在handler中使用include包含的其它的handler文件也应该位于此目录中;vars目录:应当包含一个main.yml文件,用于定义此角色用到的变量;meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系;ansible 1.3及其以后的版本才支持;default目录:为当前角色设定默认变量时使用此目录;应当包含一个main.yml文件;
三、Tags
tags用于让用户选择运行playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些代码为测试其确实没有发生变化的时间依然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此些代码片断。
实例:第一个playbook,安装httpd服务修改配置文件并启动- hosts: allremote_user: roottasks:- name: install httpd packageyum: name=httpd state=present- name: install configure filecopy: src=/working/files/httpd.conf dest=/etc/httpd/conf/- name: start httpd serviceservice: name=httpd state=started
运行playbook
ansible-playbook web.yaml
第二个playbook,调用变量,设置client hostname
1.编辑/etc/ansible/hosts文件:添加:[webservers]103.242.135.25 hname=wwww1
2.编辑hostname.yaml,内容如下:
- hosts: allremote_user: roottasks:- name: set hostnamehostname: name={{ hname }}
第三个playbook,调用变量安装程序包
- hosts: all remote_user: roottasks:- name: install {{ pservice }} serviceyum: name={{ pservice }} state=present- name: service {{ pservice }} startservice: name={{ pservice }} state=started运行playbookansible-playbook variable.yaml -e pservice=memcached
注意:调用时加-e 指定变量名对应的服务包名
playbook 注意:tags 打标签 运行playbook时加-t参数,引用配置文件中定义好的tags标签如下:
ansible-playbook -t installconf web.yaml
handlers作用:“notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作
- hosts: allremote_user: roottasks:- name: install httpd packageyum: name=httpd state=present- name: install configure filecopy: src=/working/files/httpd.conf dest=/etc/httpd/conf/tags: installconfnotify: httpd restart- name: start httpd serviceservice: name=httpd state=startedhandlers:- name: httpd restartservice: name=httpd state=restarted
实例:
- hosts: area remote_user: root tasks: - name: gd-devel yum: name=gd-devel state=present - name: pcre-devel yum: name=pcre-devel state=present - name: create otvcloud user: name=otvcloud system=true - name: create cache directory file: path=/cache state=directory owner=otvcloud group=otvcloud - name: copy iptables file copy: src=/etc/sysconfig/iptables dest=/etc/sysconfig/iptables - name: copy file copy: src=nginx-1.8.0-cache1.1.0_t4.el6.x86_64.rpm dest=/root - name: install rpm yum: name="nginx-1.8.0-cache1.1.0_t4.el6.x86_64.rpm" state=present - name: start service service: name=nginx state=started - name: start iptables service: name=iptables state=restarted
验证部署
OK,playbook Learn this to the end
- Ansible playbook
- ansible-playbook
- ansible playbook
- ansible playbook
- ansible playbook最佳实践
- ansible-playbook简介
- Ansible Playbook 介绍
- ansible之playbook入门
- Ansible playbook简单示例
- Ansible 基础入门-Playbook
- Ansible系列(5): Playbook
- Ansible playbook lookups 和 conditionals
- Ansible之playbook学习一
- ansible使用playbook安装nginx
- ansible之创建可复用playbook
- ansible-playbook批量部署nginx
- ansible playbook支持的atrributes
- ansible的playbook模式应用
- Uiautomator 2.0 VS 1.0
- hash链表自动生成宏
- 使用机顶盒录码流方法
- 技术 svn 使用简介
- C/C++ 取整函数ceil(),floor()
- ansible-playbook
- scikit-learn:0.3. 从文本文件中提取特征(tf、tf-idf)、训练一个分类器
- JDK目录结构和文件作用介绍
- Java操作excel-兼容office 2007版本之后
- Android SELinux 调试
- php实现重载
- android框架大全一
- ACM Poj1012 java版本
- 【分析方法论】属性对行为的影响分析