文章目录
练习目的:
创建一个包含主机和组的静态清单。能够创建默认和自定义静态清单。熟练掌握相应命令来列出特定要求的受管主机。
前提:
以student用户身份登录workstation
一.运行脚本
运行脚本将确保受管主机servera,serverb,serverc和serverd可在网络上访问
[student@workstation ~]$ lab deploy-inventory start
二.修改/etc/ansible/hosts以包括servera.lab.example.com作为受管主机
2.1将servera.lab.example.com添加到默认清单文件/etc/ansible/hosts的末尾
[student@workstation ~]$ sudo vim /etc/ansible/hosts
servera.lab.example.com
2.2继续编辑/etc/ansible/hosts清单文件,将[webservers]组添加到该文件的底部,并将serverb.example.com服务器作为组成员
[student@workstation ~]$ sudo vim /etc/ansible/hosts
[webservers]
serverb.lab.example.com
三.验证/etc/ansible/hosts清单文件中的受管主机
3.1列出默认清单文件中的所有受管主机
[student@workstation ~]$ ansible all --list-hosts
3.2 列出不属于某个组的受管主机
[student@workstation ~]$ ansible ungrouped --list-hosts
3.3列出属于webservers组的受管主机
[student@workstation ~]$ ansible webservers --list-hosts
四.在/home/student/deploy-inventory工作目录中,创建一个名为inventory的自定义静态清单文件
4.1创建/home/student/deploy-inventory工作目录
[student@workstation ~]$ mkdir ~/deploy-inventory
4.2在/home/student/deploy-inventory工作目录中创建inventory文件。并在该文件中写入内容
[student@workstation ~]$ cd deploy-inventory/
[student@workstation deploy-inventory]$ vim inventory
[webservers]
server[a:d].lab.example.com
[raleigh]
servera.lab.example.com
serverb.lab.example.com
[mountainview]
serverc.lab.example.com
[london]
serverd.lab.example.com
[development]
servera.lab.example.com
[testing]
serverb.lab.example.com
[production]
serverc.lab.example.com
serverd.lab.example.com
[us:children]
raleigh
mountainview
就是根据服务器清单规格里面的用途,位置,运行环境对主机名称进行分组。相同就分为一组。
五.验证自定义清单文件中的受管主机和组
5.1列出所有受管主机
[student@workstation deploy-inventory]$ ansible all -i inventory --list-hosts
5.2列出清单文件中没有分组的受管主机
[student@workstation deploy-inventory]$ ansible ungrouped -i inventory \ > --list-hosts
输入 \ 之后直接回车就会有> 符号。\是换行的意思。所以>是不用自己输入的。
5.3列出development组中所有受管主机
[student@workstation deploy-inventory]$ ansible development -i inventory --list-hosts
5.4列出testing组中所有的受管主机
[student@workstation deploy-inventory]$ ansible testing -i inventory --list-hosts
5.5列出production组的所有受管主机
[student@workstation deploy-inventory]$ ansible production -i inventory --list-hosts
5.6列出us组中所有受管主机
[student@workstation deploy-inventory]$ ansible us -i inventory --list-hosts
你自己也可以根据其他的分组条件来自定义文件中的受管主机条目。
六.完成。运行脚本清理本练习
[student@workstation ~]$ lab deploy-inventory finish
总结:使用相应命令列出符合条件的受管主机,需要在对应目录下。注意目录变化。