ansible-playbook简介


playbook是ansible进一步的运用。
中文翻译是剧本。
用另一种方式可能更容易理解,ansible的各个模块理解为linux系统下的各个命令,playbook理解成shell脚本。
playbook包含变量和任务列表:
用变量表示ansible模块调用时的各个元素,任务列表是要对这些元素的操作。
以下面的playbook为例,解释一下playbook的和各个构成,@后面跟注解:
运行方式:
ansible-playbook tomcat.yml -e "host_ip=192.168.1.1"


---                                                                        @playbook文件格式,以---开头
- hosts: "{{ host_ip }}"                                          @匹配的主机,{{}}以变量形式从命令行中接收,优先级最高
vars:                                                                    @整个book中用的变量
c_url: http://config.xbniao.com
source_path: /tmp
basedir: /usr/local
back_path: /home/xbniao/backup
projects:
- admin
- platform

tasks:                                     @任务列表
- name: stop tomcat               @第一个任务名称
tomcat: project= tomcat_{{ item }} cmd=stop                  @使用的模块,和参数
with_items: "{{ projects }}"                                               @循环变量
tags: stop                                                                        @标签,可用于人工干预playbook的任务的起始位置

- name: Backup Remote Files
command: chdir={{ basedir }}/tomcat_{{ item }}/webapps tar czf {{ back_path }}/{{ item }}-{{ ansible_date_time.date }}{{ ansible_date_time.hour }}.tar.gz ./ROOT
with_items: "{{ projects }}"
tags: bak

- name: Send Code to Remote servers
copy: src={{ source_path }}/xbniao-web-{{ item }}.war dest={{ basedir }}/tomcat_{{ item }}/webapps/ROOT/
with_items: "{{ projects }}"
tags: code

- name: unpack WAR files to ROOT/
command: chdir={{ basedir }}/tomcat_{{ item }}/webapps/ROOT /usr/java/bin/jar xf xbniao-web-{{ item }}.war
register: result
with_items: "{{ projects }}"

- name: Download Config Files
get_url:
url="{{ c_url }}/{{ item }}.config.properties"
dest=/usr/local/tomcat_{{ item }}/webapps/ROOT/WEB-INF/classes/config.properties
force=yes
with_items: "{{ projects }}"
tags: config

- name: Delete war file in ROOT
command: chdir={{ basedir }}/tomcat_{{ item }}/webapps/ROOT rm -f xbniao-web-{{ item }}.war
with_items: " {{ projects }}"
when: result|success

- name: start tomcat
tomcat: project=tomcat_{{ item }} cmd=start
with_items: "{{ projects }}"
tags: start
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值