清单定义Ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。
可以通过两种方式定义主机清单。静态主机清单可以通过文本文件定义。动态主机清单可以根据需要使用外部信息提供程序通过脚本或其他程序来生成。
静态清单文件是指定Ansible目标受管主机的文本文件。可以使用多种不同的格式编写此文件,包括INI样式或YAML。
在最简单的形式中。INI样式的静态清单文件是受管主机的主机名或IP地址的列表,每行一个:
但通常而言,可以将受管主机组织为主机组。通过主机组,可以更加有效的对一系列系统运行Ansible。这时,每一部分的开头为以中括号括起来的主机组名称。其后为该组中每一受管主机的主机名或IP地址,每行一个。
[root@localhost ~]# vi /etc/ansible/hosts
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
[root@localhost ~]# vi /etc/ansible/hosts
green.example.com
blue.example.com
192.168.100.1
192.168.100.10 //取消注释
//验证green主机是否存在于清单
[root@localhost ~]# ansible green.example.com --list-hosts
hosts (1):
green.example.com
//列出清单中的所有主机
[root@localhost ~]# ansible all --list-hosts
hosts (4):
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
[root@localhost ~]# vi /etc/ansible/hosts
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110 //取消此主机组的注释
//列出指定主机组
[root@localhost ~]# ansible webservers --list-hosts
hosts (4):
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
如果清单中含有名称相同的主机和主机组,ansible 命令将显示警告并以主机作为其目标。主机组则被忽略。
应对这种情况的方法有多种,其中最简单的是确保主机组不使用与清单中主机相同的名称。
/etc/ansible/hosts文件被视为系统的默认静态清单文件。不过,通常的做法是不使用该文件,而是在Ansible配置文件中为清单文件定义一个不同的位置。
修改清单文件位置
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# touch qingdan
[root@localhost ansible]# vi ansible.cfg
inventory = /etc/ansible/inventory //取消注释并修改位置
//写入内容
[root@localhost ansible]# vi inventory
192.168.253.132
[webservers]
192.168.253.130
192.168.253.133
//列出默认清单文件中的所有受管主机
[root@localhost ansible]# ansible all --list-hosts
hosts (3):
192.168.253.132
192.168.253.130
192.168.253.133
//列出不属于任何组的受管主机
[root@localhost ansible]# ansible ungrouped --list-hosts
hosts (1):
192.168.253.132
//列出属于某组的受管主机
[root@localhost ansible]# ansible webservers --list-hosts
hosts (2):
192.168.253.130
192.168.253.133
[root@localhost ~]# vi /etc/ansible/ansible.cfg
······
[defaults]
# some basic default values...
inventory = /etc/ansible/inventory
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22
......
Ansible运行临时命令的语法如下:
ansible host-pattern -m module [-a 'module arguments'] [-i inventory]
host-pattern参数用于指定在其上运行临时命令的受管主机。它可以是清单中的特定受管主机或主机组。也可以用后面的-i选项指定特定的清单而不使用默认清单。
-m选项将Ansible应在目标主机上运行的module名称作为参数。模块是为了实施任务而执行的小程序。一些模块不需要额外的信息,但其他模块需要使用额外的参数来指定其操作详情。-a选项以带引号字符串形式取这些参数的列表。
一种最简单的临时命令使用ping模块。此模块不执行ICMP ping,而是检查能否在受管主机上运行基于Python的模块。例如,以下临时命令确定清单中的所有受管主机能否运行标准的模块:
[root@localhost ~]# vi /etc/ansible/inventory
web1 ansible_user=root ansible_password=raylay
[root@localhost ~]# vi /etc/hosts
192.168.253.131 web1
[root@localhost ~]# ssh web1
[root@localhost ~]# ansible web1 -m ping
web1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
常用模块
模块类别 | 模块 |
---|---|
文件模块 | copy:将本地文件复制到受管主机 file:设置文件的权限和其他属性 lineinfile:确保特定行是否在文件中 synchronize:使用rsync同步内容 |
软件包模块 | package:使用操作系统本机的自动检测软件包管理器管理软件包 yum:使用yum管理软件包 apt:使用APT管理软件包 dnf:使用dnf管理软件包 gem:管理Ruby gem pip:从PyPI管理Python软件包 |
系统模块 | firewalld:使用firewalld管理防火墙 reboot:重启计算机 service:管理服务 user:添加、删除和管理用户帐户 |
Net Tools模块 | get_url:通过HTTP、HTTPS或FTP下载文件 nmcli:管理网络 uri:与Web服务交互 |
大部分模块会取用参数。可在模块的文档中找到可用于该模块的参数列表。临时命令可以通过-a选项向模块传递参数。无需参数时,可从临时命令中省略-a选项。如果需要指定多个参数,请以引号括起的空格分隔列表形式提供。
临时命令使用user模块来确保raylay用户存在于web1上并且其UID为4000:
[root@localhost ~]# ansible web1 -m user -a 'name=raylay uid=4000 state=present'
web1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 4000,
"home": "/home/raylay",
"name": "raylay",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 4000
}
[root@localhost ~]# ansible all -a 'id raylay'
web1 | CHANGED | rc=0 >>
uid=4000(runtime) gid=4000(runtime) groups=4000(runtime)