日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Inventory详解

發布時間:2023/12/25 综合教程 35 生活家
生活随笔 收集整理的這篇文章主要介紹了 Inventory详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Inventory詳解

Ansible 可以根據Inventory文件實現同時配置多個系統。這個文件位于/etc/ansible/hosts。實際使用的時候可以編寫多個Inventory文件,使用的時候用-i參數臨時指定就行。

這個文件不僅可以自己編寫、編寫多個。配置的時候不光可以使用本地的資源,也可以使用云端的資源。

這個文件可以編寫成多種格式,常用的有:INIYAML

Inventory基礎:多臺主機分組

表示同一個意思的inventory文件可以用INI寫也可以用YAML寫,不過需要安裝對應的插件。

INI格式的是這樣:

mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

方括號里面的是組名,未達到見名知義的效果,最好讓這個名字體現出:這是個什么操作系統,它被用來干什么事,一般什么時間運行。

這個文件用YAML格式寫是這樣:

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

將多臺主機配置到多個組

可以給多個組同時裝系統,于是可以做到這種效果:一臺主機既是一個web服務器,又是一個數據庫服務器

創建組的時候可以用這樣一種思路:

用來干啥

應用,棧,微服務。比如: database serversweb servers

在哪里

在哪個數據中心或哪個區域的本地的DNS服務器,storage存儲服務器。比如:eastwest

什么階段

開發階段,需要避免測試線上代碼。比如:prodtest

包含以上的三種思想的YAML格式的Inventory文件可以寫成這樣:

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
    prod:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    test:
      hosts:
        bar.example.com:
        three.example.com:

可以看到one.example.comdbservers組中,也在east組中,也在prod組中。

也可以使用嵌套的方式簡化這個文件:

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
# 以上相同        
    prod:
      children:
        east:
    test:
      children:
        west:

如果用了這種簡寫的方法,要注意變量代表它包含的所有主機。

多臺主機使用不同端口

如果有的主機沒有用默認ssh端口,那么在主機名后加上端口就行。

如果要配置的主機的端口號確實不是默認端口22,那最好還是配一下端口。

就像這樣:

badwolf.example.com:5309

如果主機的IP是靜態的,不會變更,而且連接的時候需要通過隧道連接,那可以在hosts文件中配置一下別名。

INI文件這樣配:

jumper ansible_port=5555 ansible_host=192.0.2.50
別名    端口號             主機名

YAML文件這樣配:

...
  hosts:
    jumper:
      ansible_port: 5555
      ansible_host: 192.0.2.50

如果寫入的主機名稱有統一的模式,可以這樣簡寫:

INI格式:

[webservers]
www[01:50].example.com

YAML格式:

...
  webservers:
    hosts:
      www[01:50].example.com:

對于數字的模式匹配,前導0可以根據需要加上或者去掉,前后的范圍是閉區間的,兩個邊界值都可以取到。

也可以定義字母范圍:

[databases]
db-[a:f].example.com

也可以選擇連接類型每個主機的用戶:

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan
主機                    連接類型                       連接用戶

如上所述,在inventory文件中設置這些只是一種簡化方法,稍后我們將討論如何將它們存儲在host_vars目錄中的各個文件中。

用變量指代一臺主機:主機變量

主機也可以用變量指代,而且現在指定的變量在后面的playbook中會用到。

INI格式:

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

YAML格式:

atlanta:
  host1:
    http_port: 80
    maxRequestsPerChild: 808
  host2:
    http_port: 303
    maxRequestsPerChild: 909

用變量指代多臺主機:組變量

變量也可以一次應用到整個組。

INI格式:

[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

YAML格式:

atlanta:
  hosts:
    host1:
    host2:
  vars:
    ntp_server: ntp.atlanta.example.com
    proxy: proxy.atlanta.example.com

組變量繼承:子組變量

也可以配置子組,以及使用子組變量

INI格式 YAML格式
子組 :children children:
子組變量 :vars vars:

INI格式:

[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

[usa:children]
southeast
northeast
southwest
northwest

YAML格式:

all:
  children:
    usa:
      children:
        southeast:
          children:
            atlanta:
              hosts:
                host1:
                host2:
            raleigh:
              hosts:
                host2:
                host3:
          vars:
            some_server: foo.southeast.example.com
            halon_system_timeout: 30
            self_destruct_countdown: 60
            escape_pods: 2
        northeast:
        northwest:
        southwest:

幾個默認組

有兩個默認組:allungrouped

all包含所有主機

ungrouped包含只屬于all組的主機

每個主機至少屬于兩個組

盡管allungrouped始終存在,但它們可以是隱式的,不會出現在group_names之類的組清單中。

總結

以上是生活随笔為你收集整理的Inventory详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。