• ansible 变量传递到include


    Task Include Files And Encouraging Reuse

    假设您想在play或playbook中重复使用任务列表。 您可以使用include文件来执行此操作。 使用include的任务列表是定义系统将要实现的角色的好方法。 请记住,playbook中play的目标是将一组系统映射到多个角色。 让我们看看这是什么样子

    一个task include 文件只包含一个平面的任务列表,如下所示:

    ---

    # possibly saved as tasks/foo.yml

     - name: placeholder foo

       command: /bin/foo

     - name: placeholder bar

       command: /bin/bar

    include指令看起来像这样,并且可以混合在一个playbook中的常规任务: tasks:

      - include: tasks/foo.yml 您也可以将变量传递给include。 我们称之为“参数化include”。 例如,要部署到多个wordpress实例,我可以将所有我的wordpress任务封装在一个wordpress.yml文件中,并使用如下所示:

    tasks:   - include: wordpress.yml wp_user=timmy   - include: wordpress.yml wp_user=alice   - include: wordpress.yml wp_user=bob

    从1.0开始,变量也可以使用替代语法传递到包含文件,这也支持结构化变量: tasks:

      - include: wordpress.yml     vars:         wp_user: timmy         ssh_keys:           - keys/one.txt           - keys/two.txt

    使用任何一种语法,然后可以在包含的文件中使用传入的变量。 我们将在变量中介绍它们。 你可以这样引用它们:

    {{ wp_user }} (除了明确传递的参数之外,vars部分中的所有变量也可以在这里使用。)

    include也可以在'handler'部分使用,例如,如果你想定义如何重新启动apache,你对所有的Playbook只做一次。 你可能会制作一个看似如下的handlers.yml:

    --- # this might be in a file like handlers/handlers.yml - name: restart apache   service: name=apache state=restarted

    而在你的main Playbook文件中,只要把它include进来,放到play的底部:

    handlers:   - include: handlers/handlers.yml

  • 相关阅读:
    JAVAEE学习day01
    learn MongoDB (二) 常用修改器
    learn MongoDB (一) 常用命令行操作
    js事件冒泡和捕获
    vim 常用设置
    void 0 等于 undefined 为什么不直接用undefined
    原生js操作class
    WAI-ARIA roles
    css 效果收集
    前端文章收集
  • 原文地址:https://www.cnblogs.com/gaoyuechen/p/7762466.html
Copyright © 2020-2023  润新知