ansible 教程

445课时
10.3K学过
2分

1. 总体介绍

总体介绍

Installation

从Github获取Ansible

需要安装些什么

选择哪一个版本

对管理主机的要求

对托管节点的要求

从源码运行

通过Yum安装最新发布版本

通过Apt (Ubuntu)安装最新发布版本

11 通过 Portage (Gentoo)安装最新发布版本

通过 pkg (FreeBSD)安装最新发布版本

在Mac OSX 上安装最新发布版本

通过 OpenCSW 安装最新发布版本(Solaris)

通过 Pacman 安装最新发布版本(Arch Linux)

通过 Pip 安装最新发布版本

发行版的Tarball

新手上路

前言

你的第一条命令

公钥认证

Inventory文件

主机与组1

主机与组2

主机变量

组的变量

把一个组作为另一个组的子成员

分文件定义 Host 和 Group 变量

分文件定义 Host 和 Group 变量-实例

Inventory 参数的说明

Inventory 参数的说明-实例

动态 Inventory

Cobbler 外部 Inventory 脚本

Cobbler 外部 Inventory 脚本-实例

AWS EC2 外部 inventory 脚本1

AWS EC2 外部 inventory 脚本2

其它 inventory 脚本

使用多个 inventory 源

动态组作为静态组的子组

Patterns

Introduction To Ad-Hoc Commands

Parallelism and Shell Commands1

Parallelism and Shell Commands2

File Transfer

Managing Packages

Users and Groups

Deploying From Source Control

Managing Services

Time Limited Background Operations

Gathering Facts

Ansible的配置文件

获取最新配置文件

环境配置

配置文件不同段详解

通用默认段

action_plugins

ansible_managed

ask_pass

ask_sudo_pass

bin_ansible_callbacks

callback_plugins

command_warnings

connection_plugins

deprecation_warnings

display_skipped_hosts

error_on_undefined_vars

executable

filter_plugins

force_color

force_handlers

forks

gathering

hash_behaviour

hostfile

host_key_checking

inventory

jinja2_extension

library

log_path

lookup_plugins

module_lang

module_name

nocolor

nocows

pattern

poll_interval

private_key_file

remote_port

remote_tmp

remote_user

roles_path

sudo_exe

sudo_flags

sudo_user

system_warnings

timeout

transport

98 vars_plugins

vault_password_file

Paramiko Specific Settings

record_host_keys

OpenSSH Specific Settings

ssh_args

control_path

scp_if_ssh

pipelining

Accelerated Mode Settings

accelerate_port

accelerate_timeout

accelerate_connect_timeout

accelerate_daemon_timeout

accelerate_multi_key

Windows Support

windows下的运行方式

安装管理机

Inventory

Windows System Prep

Getting to PowerShell 3.0 or higher

可用的windows模块

开发者:支持的模块及工作原理

提醒:控制机必须是Linux系统

Windows Facts

Windows Playbook Examples

Windows Contributions

2. Playbooks

Playbooks

Playbooks 简介

Playbook 语言的示例

主机与用户

Tasks 列表

Action Shorthand

Handlers:在发生改变时执行的操作

执行一个 playbook

Ansible-Pull(拉取配置而非推送配置)

提示与技巧

Playbook 角色(Roles) 和 Include 语句

Playbook 角色(Roles) 和 Include 语句-简介

Task Include Files And Encouraging Reuse

Task Include Files And Encouraging Reuse-实例1

Task Include Files And Encouraging Reuse-实例2

Roles1

Roles2

角色默认变量(Role Default Variables)

角色依赖(Role Dependencies)

在 Roles 中嵌入模块

Ansible Galaxy

Variables

合法的变量名

在Inventory中定义变量

在playbook中定义变量

在文件和role中定义变量

使用变量:关于Jinja2

Jinja2过滤器

YAML陷阱

使用Facts获取的信息

关闭Facts

本地Facts(Facts.d)

Fact缓存

注册变量

访问复杂变量数据

魔法变量,以及如何访问其它主机的信息

变量文件分割

命令行中传递变量

变量的优先级:我该在什么地方放置变量1

变量的优先级:我该在什么地方放置变量2

变量的优先级:我该在什么地方放置变量3

条件选择

When 语句

加载客户事件

在roles 和 includes 上面应用’when’语句

条件导入

基于变量选择文件和模版

注册变量

循环

标准循环

嵌套循环

对哈希表使用循环

对文件列表使用循环

对并行数据集使用循环

对子元素使用循环

对整数序列使用循环

57 随机选择

Do-Until循环.

查找第一个匹配的文件

迭代程序的执行结果

使用索引循环列表

循环配置文件

扁平化列表

循环中使用注册器

自定义迭代

最佳实践

Content Organization

Directory Layout.

Use Dynamic Inventory With Clouds

How to Differentiate Stage vs Production

Group And Host Variables

Top Level Playbooks Are Separated By Role

Task And Handler Organization For A Role

What This Organization Enables (Examples)

Deployment vs Configuration Organization

Stage vs Production

Rolling Updates

Always Mention The State

Group By Roles

Operating System and Distribution Variance

Bundling Ansible Modules With Playbooks

Whitespace and Comments

Always Name Tasks

Keep It Simple

Version Control

7. Ansible Tower

课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
4分钟

Patterns

Topics

  • Patterns

在Ansible中,Patterns 是指我们怎样确定由哪一台主机来管理. 意思就是与哪台主机进行交互. 但是在:doc:playbooks 中它指的是对应主机应用特定的配置或执行特定进程.

我们再来复习下:doc:intro_adhoc 章节中介绍的命令用法,命令格式如下:

ansible <pattern_goes_here> -m <module_name> -a <arguments>

示例如下:

ansible webservers -m service -a "name=httpd state=restarted"

一个pattern通常关联到一系列组(主机的集合) –如上示例中,所有的主机均在 “webservers” 组中.

不管怎么样,在使用Ansible前,我们需事先告诉Ansible哪台机器将被执行. 能这样做的前提是需要预先定义唯一的 host names 或者 主机组.

如下的patterns等同于目标为仓库(inventory)中的所有机器:

all
*

也可以写IP地址或系列主机名:

one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.*

如下patterns分别表示一个或多个groups.多组之间以冒号分隔表示或的关系.这意味着一个主机可以同时存在多个组:

webservers
webservers:dbservers

你也可以排队一个特定组,如下实例中,所有执行命令的机器必须隶属 webservers 组但同时不在 phoenix组:

webservers:!phoenix

你也可以指定两个组的交集,如下实例表示,执行命令有机器需要同时隶属于 webservers 和 staging 组.

webservers:&staging

你也可以组合更复杂的条件:

webservers:dbservers:&staging:!phoenix

上面这个例子表示“‘webservers’ 和 ‘dbservers’ 两个组中隶属于 ‘staging’ 组并且不属于 ‘phoenix’ 组的机器才执行命令” ... 哟!唷! 好烧脑的说!

你也可以使用变量如果你希望通过传参指定group,ansible-playbook通过 “-e” 参数可以实现,但这种用法不常用:

webservers:!{{excluded}}:&{{required}}

你也可以不必严格定义groups,单个的host names, IPs , groups都支持通配符:

*.example.com
*.com

Ansible同时也支持通配和groups的混合使用:

one*.com:dbservers

在高级语法中,你也可以在group中选择对应编号的server:

webservers[0]

或者一个group中的一部分servers:

webservers[0-25]

大部分人都在patterns应用正则表达式,但你可以.只需要以 ‘~’ 开头即可:

~(web|db).*\.example\.com

同时让我们提前了解一些技能,除了如上,你也可以通过 --limit 标记来添加排除条件,/usr/bin/ansible or /usr/bin/ansible-playbook都支持:

ansible-playbook site.yml --limit datacenter2

如果你想从文件读取hosts,文件名以@为前缀即可.从Ansible 1.2开始支持该功能:

ansible-playbook site.yml --limit @retry_hosts.txt

够简单吧. 为了更好的掌握该章节内容,可以先了解 Introduction To Ad-Hoc CommandsPlaybooks

See also

Introduction To Ad-Hoc Commands

Examples of basic commands

Playbooks

Learning ansible’s configuration management language

Mailing List

Questions? Help? Ideas? Stop by the list on Google Groups

irc.freenode.net

#ansible IRC chat channel