002.Ansible之Inventory文件


一 簡介

在使用Ansible來批量管理主機的時候,通常我們需要先定義要管理哪些主機或者主機組,而這個用於管理主機與主機組的文件就叫做Inventory,也叫主機清單。該文件默認位於/etc/ansible/hosts。當然我們也可以通過修改ansible配置文件的hostfile配置項來修改默認inventory的位置。

二 定義主機和組

有五個主機

node1  192.168.132.131  主控端

node2  192.169.132.132  被控端

node3  192.169.132.133  被控端

node4  192.169.132.134  被控端

node5  192.169.132.135  被控端

2.1 直接寫進hosts文件

對於/etc/ansible/hosts最簡單的定義格式像下面,直接把IP寫在里面:

[root@node1 ansible]# cat /etc/ansible/hosts

localhost
192.168.132.132
192.168.132.133
192.168.132.134
192.168.132.135

2.2 簡單實用ping模塊檢測連通性

[root@node1 ansible]# ansible 192.168.132.132 -m ping

ansible  指定主機或者主機組  -m 后跟使用的模塊,ping是ansible測試和被控端的連通性

是因為沒有配置密碼認證也沒有配置ssh秘鑰自動連接,主機之間不能交互

方法一:

修改配置,可以輸入密碼

[root@node1 ansible]# vim /etc/ansible/ansible.cfg

[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
#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
#module_lang    = C
#module_set_locale = False

再次執行

  

使用秘鑰自動登錄認證

恢復配置,注釋掉ack_pass

[root@node1 ansible]# ssh-copy-id root@192.168.132.132

[root@node1 ansible]# ansible 192.168.132.132 -m ping

不用輸入密碼,也可以使用模塊操作

方法三,使用hosts配置密碼

修改hosts文件

[root@node1 ansible]# cat /etc/ansible/hosts

localhost
192.168.132.132  
192.168.132.133  ansible_ssh_pass=redhat
192.168.132.134
192.168.132.135

執行

[root@node1 ansible]# ansible 192.168.132.133 -m ping

在這里可以使用這種方式推送key到被控端,然后刪掉hosts的密碼配置

2.3 實用普通用戶控制

配置普通用戶

[root@node2 ~]# useradd ansible

[root@node2 ~]# vim /etc/sudoers.d/ansible

ansible       ALL=(ALL)       NOPASSWD: ALL

配置秘鑰

[root@node2 ~]# mkdir  /home/ansible/.ssh
[root@node2 ~]# cp /root/.ssh/authorized_keys /home/ansible/.ssh/
[root@node2 ~]# chmod 600 /home/ansible/.ssh/authorized_keys
[root@node2 ~]# chown ansible:ansible /home/ansible/.ssh  -R
[root@node2 ~]# ll /home/ansible/.ssh/ -d
drwxr-xr-x 2 ansible ansible 29 Apr 27 10:52 /home/ansible/.ssh/
[root@node2 ~]# ll /home/ansible/.ssh/authorized_keys
-rw------- 1 ansible ansible 0 Apr 27 10:52 /home/ansible/.ssh/authorized_keys

主控段配置ansible用戶

[root@node1 ansible]# vim ansible.cfg

remote_user = ansible

檢查連通性

 檢查用戶,使用shell模塊

但是到133就會報錯,用戶被denied

按照相同方式配置其他幾台機器配置ansible用戶,並提權設置

2.4 hosts文件管理

使用主機名連接,則需要保證主機名可以被解析

[root@node1 ansible]# cat /etc/ansible/hosts 

localhost
node2  
192.168.132.133 
192.168.132.134
192.168.132.135

不能解析

添加/etc/hosts

[root@node1 ansible]# vim /etc/hosts

192.168.132.132  node2  

[root@node1 ansible]# ansible node2  -m shell -a "whoami"

node2 | CHANGED | rc=0 >>
ansible

也可以配置/etc/ansivle/hosts

[root@node1 ansible]# vim /etc/ansible/hosts

localhost
node2  
node3 ansible_ssh_host=192.168.132.133 
192.168.132.134
192.168.132.135

也可以使用主機名

 

但是不能使用IP

連接自己[root@node1 ansible]# ansible localhost -m ping

也可以直接指定

[root@node1 ansible]# vim /etc/ansible/hosts 

localhost  ansible_connection=local
node2  
node3 ansible_ssh_host=192.168.132.133 
192.168.132.134
192.168.132.135

三 主機分組

配置都使用主機組

3.1 簡答配置

[root@node1 ansible]# vim /etc/hosts

192.168.132.131  node1  
192.168.132.132  node2  
192.168.132.133  node3  
192.168.132.134  node4  
192.168.132.135  node5  

[root@node1 ansible]# vim /etc/ansible/hosts

[web]
node1
node2
[mysql]
node3
node4
[redis]
node5

執行

[root@node1 ansible]# ansible web -m ping

一個主機可位於多個組

[web]
node1 
node2
[mysql]  
node3 
node4
[redis]
node5
[php]
node1
node3

執行

[root@node1 ansible]# ansible php  -m  shell -a "hostname"

 

# 中括號中的名字代表組名,可以根據自己的需求將龐大的主機分成具有標識的組,如上面分了兩個組webservers和dbservers組;

# 主機(hosts)部分可以使用域名、主機名、IP地址表示;當然使用前兩者時,也需要主機能反解析到相應的IP地址,一般此類配置中多使用IP地址;

node4
[web]
node1 
node2
[mysql]  
node3 
node4
[redis]
node5
[php]
node1
node3

hosts的名字可以使用其他的文件名,但是默認找hosts文件,需要用到其他文件

[root@node1 ansible]# cp hosts inventory
[root@node1 ansible]# ansible -i inventory php -m shell -a "hostname"

也可以使用飛hosts命名的文件

3.2  指定主機范圍

# 下面指定了從web-node01到web-node50,webservers組共計50台主機;databases組有db-node-a到db-node-f共6台主機
[webservers]
web-node[01:50].test.com
[databases]
db-node[a:f].test.com

3.3 定義主機組嵌套

# 如下示例中,production組包含兩個子組,分別為webservers和dbservers,webservers和dbservers組分別包含若干主機
[webservers]
node1
node2
[dbservers]
node3

[php:children]
webservers
dbservers

執行

[root@node1 ansible]# ansible php -m shell -a "echo hostname"

node3 | CHANGED | rc=0 >>
hostname
node2 | CHANGED | rc=0 >>
hostname
node1 | CHANGED | rc=0 >>
hostname

兩個主機組都有打印

四 選擇主機與組

在前面定義Inventory的時候,我們會把所有被管理主機通過主機組的方式定義到Inventory當中,但是當我們實際使用的時候,可能只需要對某一主機或主機組進行操作,這個時候就需要通過匹配的方式指定某一特定主機或主機組。

4.1 定義主機組

在此之前,先定義一個主機清單示例:

[root@node1 ansible]# cat /etc/ansible/hosts

srv1.example.com
srv2.example.com
s1.lab.example.com
s2.lab.example.com

[web]
jupiter.lab.example.com
saturn.example.com

[db]
db1.example.com
db2.example.com
db3.example.com

[lb]
lb1.lab.example.com
lb2.lab.example.com

[boston]
db1.example.com
jupiter.lab.example.com
lb2.lab.example.com

[london]
db2.example.com
db3.example.com
file1.lab.example.com
lb1.lab.example.com

[dev]
web1.lab.example.com
db3.example.com

[stage]
file2.example.com
db2.example.com

[prod]
lb2.lab.example.com
db1.example.com
jupiter.lab.example.com

[function:children]
web
db
lb
city

[city:children]
boston
london
environments

[environments:children]
dev
stage
prod
new

[new]
172.25.252.23
172.25.252.44

14.2 匹配所有主機

可以通過all或者*來指定匹配所有主機,通過如下指令查看all匹配到的主機:

[root@node1 ansible]# ansible all --list-hosts

或者

4.3 匹配指定的主機或主機組

匹配單個組

[root@node1 ansible]# ansible prod --list-hosts

匹配單個主機

[root@node1 ansible]# ansible db2.example.com --list-hosts

  hosts (1):
    db2.example.com

匹配多個主機

 [root@node1 ansible]# ansible 'lb1.lab.example.com,s1.lab.example.com,db1.example.com' --list-hosts

  hosts (3):
    lb1.lab.example.com
    s1.lab.example.com
    db1.example.com

匹配多個組

[root@node1 ansible]# ansible 'london,boston' --list-hosts

  hosts (7):
    db2.example.com
    db3.example.com
    file1.lab.example.com
    lb1.lab.example.com
    db1.example.com
    jupiter.lab.example.com
    lb2.lab.example.com

匹配不屬於任何組的主機

[root@node1 ansible]#  ansible ungrouped --list-hosts

  hosts (4):
    srv1.example.com
    srv2.example.com
    s1.lab.example.com
    s2.lab.example.com

4.4 通配符匹配

匹配'*.example.com':

[root@node1 ansible]# ansible '*.example.com' --list-hosts

  hosts (14):
    s1.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    srv2.example.com
    db3.example.com
    srv1.example.com
    web1.lab.example.com
    db2.example.com
    db1.example.com
    jupiter.lab.example.com
    lb2.lab.example.com
    file2.example.com
    s2.lab.example.com
    saturn.example.com

匹配172.25.*的主機:

[root@node1 ansible]# ansible '172.25.*' --list-hosts

[root@node1 ansible]#  ansible '172.25.*' --list-hosts 
  hosts (2):
    172.25.252.23
    172.25.252.44

匹配以s開頭的主機及主機組:

[root@node1 ansible]# ansible 's*' --list-hosts

  hosts (7):
    file2.example.com
    db2.example.com
    s1.lab.example.com
    srv2.example.com
    srv1.example.com
    s2.lab.example.com
    saturn.example.com

4.5 通配符組合匹配

匹配包含*.example.com但不包含*.lab.example.com的主機:

 [root@node1 ansible]# ansible '*.example.com,!*.lab.example.com' --list-hosts  

  hosts (7):
    srv2.example.com
    db3.example.com
    srv1.example.com
    db2.example.com
    db1.example.com
    file2.example.com
    saturn.example.com

匹配包含prod以及172開頭、包含lab關鍵字的主機或組

[root@node1 ansible]# ansible 'prod,172*,*lab*' --list-hosts

  hosts (10):
    lb2.lab.example.com
    db1.example.com
    jupiter.lab.example.com
    172.25.252.23
    172.25.252.44
    s1.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    web1.lab.example.com
    s2.lab.example.com

匹配屬於db組同時還屬於london組的主機:

[root@node1 ansible]# ansible 'db,&london' --list-hosts

  hosts (2):
    db2.example.com
    db3.example.com

匹配在london組或者boston組,還必須在prod組中且必須不在lb組中的主機:

[root@node1 ansible]# ansible 'boston,london,&prod,!lb' --list-hosts

  hosts (2):
    db1.example.com
    jupiter.lab.example.com

4.6 正則表達式匹配

在開頭的地方使用”~”,用來表示這是一個正則表達式:

[root@node1 ansible]# ansible '~(s|db).*example\.com' --list-hosts

  hosts (8):
    s1.lab.example.com
    srv2.example.com
    db3.example.com
    srv1.example.com
    db2.example.com
    db1.example.com
    s2.lab.example.com
    saturn.example.com

 4.7 通過--limit明確指定主機或組

通過--limit在選定的組中明確指定主機:

[root@node1 ansible]# ansible ungrouped  --limit srv1.example.com --list-hosts

  hosts (1):
    srv1.example.com

通過--limit參數,還可以指定一個文件,該文件中定義明確指定的主機的列表,定義一個retry_hosts.txt如下:

[root@node1 ansible]# vim retry_hosts.txt

srv1.example.com

[root@node1 ansible]# ansible ungrouped  --limit @retry_hosts.txt --list-hosts

  hosts (1):
    srv1.example.com

4.8 通配符和正則表達式配合使用

[root@node1 ansible]# ansible '~(s|db).*,prod,*.lab.example.com' --list-hosts

  hosts (14):
    db1.example.com
    db2.example.com
    db3.example.com
    file2.example.com
    s1.lab.example.com
    srv2.example.com
    srv1.example.com
    s2.lab.example.com
    saturn.example.com
    lb2.lab.example.com
    jupiter.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    web1.lab.example.com

主機組示例結束


博主聲明:本文的內容來源主要來自譽天教育晏威老師,由本人實驗完成操作驗證,需要的博友請聯系譽天教育(http://www.yutianedu.com/),獲得官方同意或者晏老師(https://www.cnblogs.com/breezey/)本人同意即可轉載,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM