红帽RHCE之Ansible-2-部署Ansible

Ansible-2-部署Ansible

1.Ansible的Inventory文件
  • Inventory文件定义了ansible管理的主机,说白了就是Inventory文件中的内容是记录被管理的主机。
  • Inventory文件分为两种,一种是静态的Inventory文件,一种是动态的Inventory文件。
    • 静态的Inventory文件是很好理解的,就是txt文本记录的被管理主机,只要不修改这个静态Inventory文件,被管理的主机就不会发生变化。
    • 动态的Inventory文件是能动态输出被管理主机,动态Inventory文件的原理就是一个脚本,大部分都是python脚本。动态Inventory文件能连接到某个管理系统的节点信息数据库,并将节点信息以特定的格式输出(绝大多数情况下是json),也就是说动态Inventory文件输出的主机信息是管理系统节点信息数据库的内容,也就是说节点信息数据库的变化就会导致Inventory输出的被管理主机数量不同。所以称之为动态的Inventory文件。
2.如何查看一个Inventory文件中的主机信息
  • ansible命令-i参数可以指定Inventory文件的路径,由于在你的ansible控制节点里面可能有多个Inventory文件,所以使用正确的Inventory文件就非常有必要。当然还有其他方式可以指定Inventory文件的路径,但是-i参数的优先级是最高的。
[student@workstation ~]$ vim inventory
[student@workstation ~]$ cat inventory
[storage]
servera
serverb

[compute]
serverc
serverd

[cloud:children]
storage
compute

//storage表示storage主机组
//--list-hosts表示列出你想查看的主机
[student@workstation ~]$ ansible -i /home/student/inventory storage --list-hosts
  hosts (2):
    servera
    serverb
[student@workstation ~]$ ansible -i /home/student/inventory cloud --list-hosts
  hosts (4):
    servera
    serverb
    serverc
    serverd
[student@workstation ~]$ ansible -i /home/student/inventory compute --list-hosts
  hosts (2):
    serverc
    serverd
3.Inventory文件的练习
[student@workstation ~]$ ssh workstation
//开始练习
[student@workstation ~]$ lab deploy-inventory start

Setting up workstation for lab exercise work:

 · Verify ansible is installed on workstation control node.....  SUCCESS
 · Backup /etc/ansible/hosts inventory.........................  SUCCESS

///etc/ansible/hosts是ansible配置文件指定Inventory默认的文件,
//如果使用ansible命令不加-i参数,默认就会使用这个Inventory文件。
[student@workstation ~]$ sudo vim /etc/ansible/hosts
[student@workstation ~]$ cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [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

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com
servera.lab.example.com

[webservers]
serverb.lab.example.com

//检查主机
[student@workstation ~]$ ansible all --list-hosts
  hosts (2):
    servera.lab.example.com
    serverb.lab.example.com
[student@workstation ~]$ ansible ungrouped --list-hosts
  hosts (1):
    servera.lab.example.com
[student@workstation ~]$ ansible webservers --list-hosts
  hosts (1):
    serverb.lab.example.com

[student@workstation deploy-inventory]$ vim inventory 
[student@workstation deploy-inventory]$ cat inventory 
[development]
servera.lab.example.com
[testing]
serverb.lab.example.com
[production]
server[c:d].lab.example.com

[raleigh]
server[a:b].lab.example.com

[mountainview]
serverc.lab.example.com

[london]
serverd.lab.example.com

[webserver]
server[a:d].lab.example.com

[us:children]
raleigh
mountainview

[student@workstation deploy-inventory]$ ansible -i inventory all --list-hosts
  hosts (4):
    servera.lab.example.com
    serverb.lab.example.com
    serverc.lab.example.com
    serverd.lab.example.com

[student@workstation deploy-inventory]$ ansible -i inventory ungrouped --list-hosts
 [WARNING]: No hosts matched, nothing to do

  hosts (0):

//完成练习
[student@workstation ~]$ lab deploy-inventory finish

Cleanup workstation control node

 · Restore /etc/ansible/hosts..................................  SUCCESS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值