Playbooks

[TOC]

示例

# --- 表示 yaml 文档开始
---
# 第一个场景(PLAY)
# 注意多种 host 的写法
- hosts: A,B
  remote_user: root
  # 需要执行的任务(TASK)
  tasks:
  # 为任务命名(可省略,但建议写上)
  - name: Ping the host
    # 调用的模块
    ping:
  - name: mkdir /root/test
    file:
      # 模块的参数
      path: /root/test
      state: directory

# 第 2 个场景(PLAY)
- hosts:
    A
    B
  remote_user: root
  tasks:
  - name: rmdir /root/test
    file:
      path: /root/test
      state: absent

handlers

handler 使用

handler 组

tag

variable

vars 变量

facts 变量

用户输入变量

命令行变量

不同 play 变量

包含变量

循环

判断

when

test

block

过滤

字符相关

数字相关

列表相关

默认值

分析 json

常用

include

include

include_tasks

include_tasksinclude基本相同,但是涉及到 tag标签,有所不同。详情见ansible笔记(37):include(二)

import_tasks

"import_tasks" 是静态的,"include_tasks" 是动态的。 "静态"的意思就是被 include 的文件在 playbook 被加载时就展开了(是预处理的)。 "动态"的意思就是被 include 的文件在 playbook 运行时才会被展开(是实时处理的)。 由于"include_tasks"是动态的,所以,被 include 的文件的文件名可以使用任何变量替换。 由于"import_tasks"是静态的,所以,被 include 的文件的文件名不能使用动态的变量替换。

  1. 循环 使用"loop"关键字或"with_items"关键字对 include 文件进行循环操作时,只能配合"include_tasks"才能正常运行。

  2. when判断 当对"include_tasks"使用 when 进行条件判断时,when 对应的条件只会应用于"include_tasks"任务本身,当执行被包含的任务时,不会对这些被包含的任务重新进行条件判断。 当对"import_tasks"使用when进行条件判断时,when 对应的条件会应用于被 include 的文件中的每一个任务,当执行被包含的任务时,会对每一个被包含的任务进行同样的条件判断。

  3. tag 与"include_tasks"不同,当为"import_tasks"添加标签时,tags 是针对被包含文件中的所有任务生效的,与"include"关键字的效果相同。

  4. handers "include_tasks"与"import_tasks"都可以在 handlers 中使用,并没有什么不同

include_playbook

使用"include"引用整个playbook,在之后的版本中,如果想要引入整个 playbook,则需要使用"import_playbook"模块代替"include"模块, 因为在 2.8 版本以后,使用"include"关键字引用整个 playbook 的特性将会被弃用。

Template

template

jinja

转义

Galaxy

安装

requirements

创建 Role

操作Role

初识 playbook

handler 详解

变量

循环

判断

ansible过滤器

include

jinja2 模板

ansible-galaxy 官网

Ansible Galaxy 使用小记

ANSIBLE GALAXY

Last updated

Was this helpful?