02-Ansible 基本使用

Ansible 清单

Ansible 软件包中文件

bash 复制代码
[laoma@controller ~]# rpm -ql ansible
  • 配置文件目录 /etc/ansible
  • 执行文件目录/usr/bin
  • lib依赖库目录/usr/lib/python2.7/site-packages/ansible
  • 插件/usr/share/ansible/plugins
  • Help文档目录/usr/share/doc/ansible
  • Man文档目录/usr/share/man/

主机清单

Inventory 定义Ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理 组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。

通过以下方式定义主机清单:

  • 静态主机清单:以文本文件的方式来定义。
  • 动态主机清单 :使用外部信息提供程序通过脚本或其他程序来自动生成。目的是从启动环境中获取主机清单,例如openstack、kubernetes、zabbix等。

静态主机清单

主机清单支持多种格式,例如ini、yaml、脚本等。

本次课程使用 ini 格式。

最简单的静态清单

受管节点的主机名或IP地址的列表,每行一个。

示例:

bash 复制代码
[laoma@controller ~]$ vim inventory
web1.example.com
web2.example.com
db1.example.com
db2.example.com
192.0.2.42

验证主机是否在inventory中

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory web1.example.com
hosts (1):
web1.example.com
[laoma@controller ~]$ ansible --list-hosts -i inventory 192.0.2.42
hosts (1):
192.0.2.42

ansible命令通过--inventory PATHNAME或-i PATHNAME选项在命令行中指定清单文件的位置,其中PATHNAME是所需清单文件的路径。

主机组

还可以将受管节点组织为主机组。通过主机组,更加有效地对一系列系统运行Ansible。

格式:

bash 复制代码
[groupname]
hostname
hostip

示例:

bash 复制代码
app1.example.com
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
192.0.2.42
192.0.2.43

验证:

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory webservers
hosts (2):
web1.example.com
web2.example.com
# 注意:192.0.2.43属于dbservers组
[laoma@controller ~]$ ansible --list-hosts -i inventory dbservers
hosts (4):
db1.example.com
db2.example.com
192.0.2.42
192.0.2.43

有两个组总是存在的:

  • all:包含inventory中所有主机。
  • ungrouped:inventory中列出的,但不属于任何组的主机。

验证:

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory all
hosts (7):
app1.example.com
web1.example.com
web2.example.com
db1.example.com
db2.example.com
192.0.2.42
192.0.2.43
[laoma@controller ~]$ ansible --list-hosts -i inventory ungrouped
hosts (1):
app1.example.com

根据需要,将主机分配在多个组中,例如根据主机的角色、其物理位置以及是否在生产环境中等因素。

bash 复制代码
[webservers]
web1.example.com
web2.example.com
192.168.3.7
[dbservers]
db1.example.com
db2.example.com
192.0.2.42
[eastdc]
web1.example.com
db1.example.com
[westdc]
web2.example.com
db2.example.com

验证:

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory webservers
hosts (3):
web1.example.com
web2.example.com
192.168.3.7
[laoma@controller ~]$ ansible --list-hosts -i inventory eastdc
hosts (2):
web1.example.com
db1.example.com

主机组嵌套

一个主机组还可以属于另外一个主机组。

示例:

bash 复制代码
[eastdc]
web1.example.com
db1.example.com
[westdc]
web2.example.com
db2.example.com
[dc:children]
eastdc
westdc

验证:

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory dc
hosts (4):
web1.example.com
db1.example.com
web2.example.com
db2.example.com

子组中的主机组必须定义,否则会出现语法上的报错。

示例:

bash 复制代码
[eastdc]
web1.example.com
db1.example.com
[westdc]
web2.example.com
db2.example.com
[dc:children]
eastdc
westdc
node1

验证:

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory dc
[WARNING]: * Failed to parse /home/laoma/inventory with yaml plugin:
Syntax
Error while loading YAML. did not find expected <document start> The
error
appears to be in '/home/laoma/inventory': line 2, column 1, but may be
elsewhere
in the file depending on the exact syntax problem. The offending line
appears to
be: [eastdc] web1.example.com ^ here
[WARNING]: * Failed to parse /home/laoma/inventory with ini plugin:
/home/laoma/inventory:12: Section [dc:children] includes undefined
group:
node1
[WARNING]: Unable to parse /home/laoma/inventory as an inventory
source
[WARNING]: No inventory was parsed, only implicit localhost is
available
[WARNING]: provided hosts list is empty, only localhost is available.
Note that
the implicit localhost does not match 'all'
hosts (4):
web1.example.com
db1.example.com
web2.example.com
db2.example.com

范围简写

通过指定主机名称或IP地址的范围来简化Ansible主机清单。您可以指定数字或字母范围。

语法:[start:end]

示例:

bash 复制代码
# 代表192.168.4.0-192.168.7.255
[priv]
192.168.[4:7].[0:255]
#代表01,02...20
[hosts]
host[01:20].example.com
# 代表a b c
[servers]
server[a:c].example.com

验证:

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory hosts
hosts (20):
host01.example.com
host02.example.com
......
host19.example.com
host20.example.com
[laoma@controller ~]$ ansible --list-hosts -i inventory priv
hosts (1024):
192.168.4.0
192.168.4.1
192.168.4.2
......
192.168.7.253
192.168.7.254
192.168.7.255
[laoma@controller ~]$ ansible --list-hosts -i inventory servers
hosts (3):
node1.example.com
node2.example.com
serverc.example.com

以下是错误的范围示例:

bash 复制代码
[servers]
server[0a:2c].example.com

验证:

bash 复制代码
[laoma@controller ~]$ ansible --list-hosts -i inventory all
[WARNING]: * Failed to parse /home/laoma/inventory with yaml plugin:
Syntax
Error while loading YAML. did not find expected <document start> The
error
appears to be in '/home/laoma/inventory': line 2, column 1, but may be
elsewhere
in the file depending on the exact syntax problem. The offending line
appears to
be: [servers] server[0a:2c].example.com ^ here
[WARNING]: * Failed to parse /home/laoma/inventory with ini plugin:
invalid
literal for int() with base 10: '0a'
[WARNING]: Unable to parse /home/laoma/inventory as an inventory
source
[WARNING]: No inventory was parsed, only implicit localhost is
available
[WARNING]: provided hosts list is empty, only localhost is available.
Note that
the implicit localhost does not match 'all'
hosts (0):

动态主机清单

使用外部数据提供的信息动态生成Ansible清单信息。

本课程内容不做进一步讨论。

相关推荐
cly13 天前
Ansible自动化(十五):加解密详解
运维·自动化·ansible
cly13 天前
Ansible自动化(十二):Jinja2过滤器
运维·自动化·ansible
cly13 天前
Ansible自动化(十三):调试与优化
数据库·自动化·ansible
cly13 天前
Ansible自动化(十四):Roles(角色)
服务器·自动化·ansible
cly13 天前
Ansible自动化(十一):Jinja2模板
网络·自动化·ansible
cly13 天前
Ansible自动化(八):条件语句
运维·自动化·ansible
China_Yanhy3 天前
Ansible 工业级项目标准化架构指南 (V1.0)
架构·ansible
cly13 天前
Ansible自动化(九):循环语句
windows·自动化·ansible
cly13 天前
Ansible自动化(十):配置文件管理模块(lineinfile / blockinfile)
运维·自动化·ansible