ansible应用合集

李松涛-Ansible系列课程-基础入门-InventoryFi

2015-12-21  本文已影响1399人  stanleylst
李松涛-Ansible系列课程-基础入门-InventoryFile目录.png
难度指数:** ****5**星(满星5星)
技术指数:** ****5**星(满星5星)
理论指数: **3**星(满星5星)
面向人群: 自动化运维&初中级运维

1. Ansible程序目录功能介绍

rpm -ql ansible

通过如上命令可获取ansible所有文件存放目录,输出内容较多但大致分为如下几类:

** 作用 **

  1. Hosts & groups inventory配置
  2. Ansible 功能特性自定义,如:
    a) Inventory默认文件位置
    b) 默认library依赖库定义
    c) 命令远程默认执行目录
    d) Ssh默认连接端口

下面相继为大家着重介绍如上两部分

2. ansible“嫡系”命令功能简介

键入ansible后连续两次Tab会发现补全了如下命令,我们逐一介绍

ansible-galaxy
ansible-pull
ansible-doc
ansible-playbook
ansible-vault

2.1 ansible

ansible是使用率非常高的命令之一,man中是如此定义其功能的 run a command somewhere else 可见其灵活性. 针对:

# ansible web1 –m ping //检查服务器存活
# ansible web1 –m copy –a “src=/etc/fstab dest=/etc/fstab owner=root group=root mode=644 backup=yes” //复制本地文件到远程

2.2 ansible-galaxy

和三星没关系,你可以把他理解成ansible的pip,可以从galaxy.ansible.com下载官方收录的playbooks

https://galaxy.ansible.com/list#/roles?page=1&page_size=10

我们以第一个名为 git 的 roles为例:

# ansible-galaxy --ignore-errors install azavea.git

随机试用如下两个可正常下载.

# ansible-galaxy install hectcastro.nginx
# ansible-galaxy install Juniper.junos --ignore-errors

报错 - the API server (galaxy.ansible.com) is not responding, please try again later.
需要确认下自己的网络是否能正常翻墙

下载好的roles默认存放:

/etc/ansible/roles/XXX

实测下来不是特别灵活,实战案例推荐下载:

https://github.com/ansible/ansible-examples

2.3 ansible-pull

该指令使用需要谈到ansible的另一种模式---pull 模式,这和我们平常经常用的push模式刚好相反,其适用于以下场景:你有数量巨大的机器需要配置,即使使用非常高的线程还是要花费很多时间;你要在一个没有网络连接的机器上运行Anisble,比如在启动之后安装。这部分也会单独做一节来讲。
** 实现工具:**

Ansible-pull + git + crontab

实现原理:

Ansible pull 通过crontab定期拉取指定的git 版本到本地, 以指定模式自运行事先预定的指令.

点评:

主要在大批量机器场景下会使用,灵活性稍有欠缺,但效率上无效提升,总体对运维人员的技术和前瞻性规划有更高要求

2.4 ansible-doc

如你所料,ansible-doc是ansible模块文档说明,针对每个模块都是详细的用法说明及应用案例介绍.

# ansible-doc –l   //列出支持的模块
# ansible-doc 模块  //模块功能说明

2.5 ansible-playbook

该工具是使用频率最高的工具,工作机制是通过读取预先编写好的playbook文件来实现批量管理.
Playbook编写简单,可定制性高,灵活方便同时可固化

2.6 ansible-vault

Ansible-vault如果你的配置文件中含有敏感信息,你可能并不希望他能被人看到,vault可以帮你加密/解密这个配置文件,高级用法,请参照http://www.ansible.com/blog/2014/02/19/ansible-vault

在企业实践中应用较少,后面会作为功能扩展部分介绍

3. 管理Inventory file

Inventory file 即定义ansible管理主机的配置文件. Ansible为方便批量管理主机,便捷使用其中的部分主机,我们可以在Inventory file中按需对主机进行group分组.默认的inventory file为/etc/ansible/hosts .
Inventory file可以多个,通过 –i 或 –inventory-file 指定读取,同时可动态生成 inventory file, 如 AWS EC2 , cobbler

3.1 主机和组

主机以行为单位,ip或hostname
组中括号标识组名

192.168.37.149
192.168.37.153

[websevers]
web1
web[2:9]
web[10:20]
web153

[dbservers]
db-a.example.com
db-[b:f].example.com

//针对 web153 单台机器执行 ping存活检测

 # ansible web153 –m ping

//针对webservers 组中所有主机执行 ping 存活检测

# ansible websevers –m ping

3.2 主机变量

主机变量: 针对单主机的特性化要求,通过内置变量实现

[webservers]
web1.magedu.com http_port=80 maxRequestsPerChild=808

3.3 组变量

组变量针对大量机器的变量需求,通过指定组变量的设定简单方便

[groupservers]
web1.magedu.com
web2.magedu.com

[groupservers:vars]
ntp_server=ntp.magedu.com
nfs_server=nfs.magedu.com

3.4 组嵌套及组变量

组嵌套组与组之间可以相互调用,并且可以向组中的主机指定变量(组变量)

[apache]
httpd1.magedu.com
httpd2.magedu.com

[nginx]
ngx1.magedu.com
ngx2.magedu.com

[webservers:children]
apache
nginx

[webservers:vars]
ntp_server=ntp.magedu.com

3.5 变量分离

变量除了可以和inventory一起存储在INI配置文件中,也可以独立出来如inventory方式存储在单独的配置文件中.
假设:

Inventory file 存储在默认位置 /etc/ansible/hosts
group_vars: /etc/ansible/group_vars/组变量文件名.(yml|yaml|json)
host_vars: /etc/ansible/host_vars/主机变量文件名. (yml|yaml|json)

/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

3.6 支持参数列表

Inventory文件支持的参数列表参考如下:

ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass
ansible_sudo_pass
ansible_connection
ansible_ssh_private_key_file
ansible_sftp_extra_args
ansible_scp_extra_args
ansible_ssh_extra_args
ansible_ssh_pipelining
ansible_shell_type
ansible_python_interpreter

更详细信息篇幅原因参考官网
http://docs.ansible.com/ansible/intro_inventory.html#groups-of-groups-and-group-variables

4. Ansible Patterns

Patterns 功能类似正则匹配,对于主机管理灵活性有着极大帮助.
开始之前先来了解 ansible 用法:

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

示例:

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

4.1 pattern技巧介绍:

4.1.2 all 技巧:

all 等同于 *

# ansible all –m ping
# ansible “*” -m ping   //这里需要有加引号

4.1.3 or 技巧

同时执行多个主机或主机组, 相互之间用 : 分隔
web1:web2

# ansible web1: web2 -m ping  

4.1.4 逻辑非 技巧

! 表示非,
webservers:!phoenix

所有的主机在webservers中同时不在 phoenix中

4.1.5 逻辑与

& 逻辑与/并且
webservers:&staging
所有的主机隶属于 webservers group且同时存在于 staging group

4.1.6 多条件组合

webservers:dbservers:&staging:!phoenix

如上条件组合表示:
webservers group 和 dbservers group的所有主机staging 中存在且 phoenix 中不存在的主机才会被管理。

当然, ansible & ansible-play 提供标准参数读取方式., -e 来实现标准输入的读取。
在命令行中,可以通过变量的方式

4.1.7 模糊匹配

*.example.com
*.com
one*.com:dbservers

不需要精确的指定

4.1.8 域切割

[webservers]
cobweb
webbing
weber

因为底层python实现,所以大家看到这个功能应该很熟悉,很强大

webservers[0]       # == cobweb
webservers[-1]      # == weber
webservers[0:1]     # == webservers[0],webservers[1]
                    # == cobweb,webbing
webservers[1:]      # == webbing,weber

4.1.9 正则匹配

以 ~ 开始,表示正则匹配

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

# ansible "~(beta|web|green)\.example\.(com|org)" -m ping
1.png
# ansible ~192\.168\.[0-9]\{\2}.[0-9]\{2,} -m ping   
2.png

Ok, inventory的功能用法经过上面的介绍相信大家有所了解,功能强大但不失灵活,同时也是日常工作中最为基础且最为常用的。较新手来讲没有接触过正则表达式相对需要掌握的较多,有过一定基础的只当是知识体系的回顾了

上一篇下一篇

猜你喜欢

热点阅读