1. include任务复用

有时,我们发现大量的 Playbook 内容需要重复编写,各 Tasks 之间功能需相互调用才能完成各自功能, Playbook 庞大到维护困难,这时我们需要使用 include

include支持在tasks阶段和handles阶段调用,调用多个include时可以使用loop循环的方式,还可以向includeplaybook中传递变量。

比如:A项目需要用到重启 httpd ,B项目需要用到重启 httpd ,那么我们可以使用 Include 来减少重复编写。

1.1 多个项目调用相同task

  • 编写 restart_httpd.yml 文件:

    #注意这是一个tasks所有没有play的任何信息
    [root@ansible project1]# cat restart_httpd.yml
    - name: Restart Httpd Server
    service:
    name: httpd
    state: restarted
  • A Projectplaybook 如下:

    [root@ansible project1]# cat a_project.yml
    - hosts: webserver
    tasks:
    - name: A Project command
    command: echo "A" - name: Restart httpd
    include: restart_httpd.yml
  • B Projectplaybook 如下:

    [root@ansible project1]# cat b_project.yml
    - hosts: webserver
    tasks:
    - name: B Project command
    command: echo "B" - name: Restart httpd
    include: restart_httpd.yml
  • playbook运行如下:

    [root@ansible project1]# ansible-playbook a_project.yml
    [root@ansible project1]# ansible-playbook b_project.yml

1.2 Inlcude结合tags应用

”include”不仅能够引用任务列表,还能够引用playbook,比如,在一个playbook中引用另一个playbook

示例:通过指定标签 tags ,来说明是安装 tomcat8 还是 tomcat9

1.准备入口 main.yml 文件,然后包含 install_tomcat8.yml 以及install_tomcat9.yml

2.在执行 main.yml 时,需要通过 --tags 指明要安装的版本

3.还可以在主playbook文件中向引用的playbook传递变量。

  • 编写main.yml入口文件:

    #注意:引用playbook时建议使用import_playbook参数:官方提示:[DEPRECATION WARNING]: 'include' for playbook includes. You should use 'import_playbook' instead. This feature will be removed in version 2.12. 
    
    [root@xuzhichao playbook]# cat tomcat_main.yml
    - name: Install Tomcat8
    import_playbook: install_tomcat8.yml
    tags: tomcat8
    vars:
    tomcat_version: 8.5.69
    tomcat_install_path: /usr/local - name: Install Tomcat9
    import_playbook: install_tomcat9.yml
    tags: tomcat9
    vars:
    tomcat_version: 9.0.50
    tomcat_install_path: /usr/local
  • 编写install_tomcat8.yml文件:

    [root@xuzhichao playbook]# cat install_tomcat8.yml
    ---
    - hosts: localhost tasks:
    - name: Install JDK
    yum:
    name: java-1.8.0-openjdk
    state: present - name: Download Tomacat
    get_url:
    url: https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
    dest: /root - name: Unarchive Tomcat
    unarchive:
    src: /root/apache-tomcat-{{ tomcat_version }}.tar.gz
    dest: "{{ tomcat_install_path }}" - name: Create Link File
    file:
    src: "{{ tomcat_install_path }}/apache-tomcat-{{ tomcat_version }}"
    dest: "{{ tomcat_install_path }}/tomcat8"
    state: link - name: Start Tomcat
    shell: cd "{{ tomcat_install_path }}"/tomcat8/bin && nohup ./startup.sh &
  • 编写install_tomcat9.yml文件:

    [root@xuzhichao playbook]# cat install_tomcat9.yml
    ---
    - hosts: localhost tasks:
    - name: Install JDK
    yum:
    name: java-1.8.0-openjdk
    state: present - name: Download Tomacat
    get_url:
    url: https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
    dest: /root - name: Unarchive Tomcat
    unarchive:
    src: /root/apache-tomcat-{{ tomcat_version }}.tar.gz
    dest: "{{ tomcat_install_path }}" - name: Create Link File
    file:
    src: "{{ tomcat_install_path }}/apache-tomcat-{{ tomcat_version }}"
    dest: "{{ tomcat_install_path }}/tomcat9"
    state: link - name: Start Tomcat
    shell: cd "{{ tomcat_install_path }}"/tomcat9/bin && nohup ./startup.sh &
  • 运行playbook文件:

    #安装tomcat9
    [root@xuzhichao playbook]# ansible-playbook -t tomcat9 tomcat_main.yml #安装tomcat8
    [root@xuzhichao playbook]# ansible-playbook -t tomcat8 tomcat_main.yml

ansible系列(27)--ansible的include任务复用的更多相关文章

  1. Ansible系列(五):playbook应用和roles自动化批量安装示例

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  2. Ansible系列(四):playbook应用和roles自动化批量安装示例

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...

  3. Ansible系列(三):YAML语法和playbook写法

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  4. Ansible系列(二):选项和常用模块

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  5. Ansible系列(一):基本配置和使用

    本文目录:1.1 安装Ansible1.2 配置Ansible 1.2.1 环境配置 1.2.2 SSH互信配置 1.2.3 简单测试1.3 inventory Ansible是一种批量.自动部署工具 ...

  6. Ansible系列(六):各种变量定义方式和变量引用

    本文目录:1.1 ansible facts1.2 变量引用json数据的方式 1.2.1 引用json字典数据的方式 1.2.2 引用json数组数据的方式 1.2.3 引用facts数据1.3 设 ...

  7. Ansible系列(六):循环和条件判断

    本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...

  8. Ansible系列(七):执行过程分析、异步模式和速度优化

    本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...

  9. Ansible系列(五):各种变量定义方式和变量引用

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...

  10. 自动化运维工具ansible学习+使用ansible批量推送公钥到远程主机

    目录: 一.ansible简介 1.1.ansible是什么 1.2.ansible如何工作 1.3.ansible优缺点 1.4.ansible安装方式 1.5.ansible文件简单介绍 1.6. ...

随机推荐

  1. KingbaseES 数据库连接

    一.数据准备: create table student( id int , s_name varchar(20), t_id int ); create table teacher( id int ...

  2. python打包Windows.exe程序(pyinstaller)

    python打包Windows.exe程序(pyinstaller) 基础命令 pip install pyinstaller 使用pip命令来安装pyinstaller模块. -F: pyinsta ...

  3. C++设计模式 - 职责链模式(Chain of Resposibility)

    数据结构模式 常常有一-些组件在内部具有特定的数据结构,如果让客户程序依赖这些特定的数据结构,将极大地破坏组件的复用.这时候,将这些特定数据结构封装在内部,在外部提供统一的接口,来实现与特定数据结构无 ...

  4. 浅谈ET框架--ECS设计核心(一)

    ET框架的ECS设计核心可以总结为一句话,那就是: 继承转组件,多态转分发 OOP设计里的继承更换为组件Component模式,多态转成分发模式. 框架代码里头的案例: 数值组件挂载Entity上. ...

  5. #Splay#U137476 序列

    题目 给定长度为\(n\)的序列\(Ai\) ,我们将按照如下操作给\(Ai\) 排序, 先找到编号最小的所在位置\(x1\) ,将\([1,x1]\) 翻转, 再找到编号第二小的所在位置\(x2\) ...

  6. HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态

    [导读] 随着人工智能不断发展,机器学习技术也开始被广泛地应用到教育.金融.零售.交通.医疗等各个领域,给我们的生活带来巨大的便利.本期Discovery直播以<与虎墩一起,玩转AI新" ...

  7. keycloak~对架构提供的provider总结

    提供者目录 Provider Authenticator BaseDirectGrantAuthenticator AbstractFormAuthenticator AbstractUsername ...

  8. 【直播合集】HDC.Together 2023 精彩回顾!收藏勿错过~

    HDC.Together 2023 主题演讲 万象复兴,热潮澎湃,HarmonyOS 全面进化,迈入新纪元.以创新改变世界,以生态驱动未来.扬帆起航,就在此刻.新版本.新体验.新流量.新商业.新机遇. ...

  9. 官方直播丨“Hello Ability:从页面跳转开始”周三晚不见不散

    12月8日 19:00-20:30,Hello HarmonyOS系列课程的第四期"Hello Ability:从页面跳转开始"线上直播,将带你学习如何快速通过JS page间.A ...

  10. 为你推荐一款高效的IO组件——okio

    原文:https://mp.weixin.qq.com/s/XnNhSq8ESoslb2DQEzMbCQ,点击链接查看更多技术内容.   前不久,三方组件库上新了一批JS/eTS组件,其中就包括oki ...