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
发布了91 篇原创文章 · 获赞 25 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/kevin3101/article/details/69390195