ansible之inventory文件

Ansible 可同時操作屬於一個組的多臺主機,組和主機之間的關係通過 inventory 文件配置. 默認的文件路徑爲 /etc/ansible/hosts

主機與組

/etc/ansible/hosts 文件的格式與windows的ini配置文件類似:

mail.example.com

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

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

方括號是組名,方面調用。

一個系統可以屬於不同的組,比如一臺服務器可以同時屬於 webserver組 和 dbserver組.這時屬於兩個組的變量都可以爲這臺主機所用,至於變量的優先級關係將於以後的章節中討論.

  • 當有特殊端口需求時,可以指定端口:
badwolf.example.com:5309
  • 手動主機名設置(此主機名不在系統hosts文件中):
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50

在這個例子中,通過 “jumper” 別名,會連接192.168.1.50:5555
記住,這是通過 inventory 文件的特性功能設置的變量. 一般而言,這不是設置變量(描述你的系統策略的變量)的最好方式.後面會說到這個問題。

  • 一組相似的host
[databases]
db-[a:f].example.com

或者

[webservers]
www[01:50].example.com
  • 對於每個節點,可以單獨設置用戶名和連接類型:
[targets]

localhost           ansible_connection=local
other1.example.com  ansible_connection=ssh  ansible_ssh_user=mpdehaan
other2.example.com  ansible_connection=ssh  ansible_ssh_user=mdehaan

所有以上討論的對於 inventory 文件的設置是一種速記法,後面我們會討論如何將這些設置保存爲 ‘host_vars’目錄中的獨立的文件。

主機變量

前面已經提到過,分配變量給主機很容易做到,這些變量定義後可在 playbooks 中使用:

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

組的變量

定義屬於整個組的變量:

[atlanta]
host1
host2

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

把一個組做爲另一個組的子成員

可以把一個組作爲另一個組的子成員,以及分配變量給整個組使用. 這些變量可以給 /usr/bin/ansible-playbook 使用,但不能給 /usr/bin/ansible 使用。

如果你需要存儲一個列表或hash值,或者更喜歡把 host 和 group 的變量分開配置,請看下一節的說明。

把組和成員分開配置

上面說了不推薦在inventory文件裏直接寫屬性,因爲可以通過和inventory分開的文件來定義這些屬性。這些文件格式爲yaml,其語法在後面說。

假設inventory文件爲

/etc/ansible/hosts

假設有一個主機名爲 ‘foosball’, 主機同時屬於兩個組,一個是 ‘raleigh’, 另一個是 ‘webservers’. 那麼以下配置文件(YAML 格式)中的變量可以爲 ‘foosball’ 主機所用.依次爲 ‘raleigh’ 的組變量,’webservers’ 的組變量,’foosball’ 的主機變量:

/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

舉例來說,假設你有一些主機,屬於不同的數據中心,並依次進行劃分.每一個數據中心使用一些不同的服務器.比如 ntp 服務器, database 服務器等等. 那麼 raleigh 這個組的組變量定義在文件 /etc/ansible/group_vars/raleigh 之中,可能類似這樣:

---  #(三個'-')
ntp_server: acme.example.org
database_server: storage.example.org
  • 這些定義變量的文件不是一定要存在,因爲這是可選的特性。

還有更進一步的運用,你可以爲一個主機,或一個組,創建一個目錄,目錄名就是主機名或組名.目錄中的可以創建多個文件, 文件中的變量都會被讀取爲主機或組的變量.如下 ‘raleigh’ 組對應於 /etc/ansible/group_vars/raleigh/ 目錄,其下有兩個文件 db_settings 和 cluster_settings, 其中分別設置不同的變量:

$ ls   /etc/ansible/group_vars/raleigh/
$ db_settings      db_settings

inventory參數

如同前面提到的,通過設置下面的參數,可以控制 ansible 與遠程主機的交互方式:

ansible_ssh_host
      將要連接的遠程主機名.與你想要設定的主機的別名不同的話,可通過此變量設置.

ansible_ssh_port
      ssh端口號.如果不是默認的端口號,通過此變量設置.

ansible_ssh_user
      默認的 ssh 用戶名

ansible_ssh_pass
      ssh 密碼(這種方式並不安全,我們強烈建議使用 --ask-pass 或 SSH 密鑰)

ansible_sudo_pass
      sudo 密碼(這種方式並不安全,我們強烈建議使用 --ask-sudo-pass)

ansible_sudo_exe (new in version 1.8)
      sudo 命令路徑(適用於1.8及以上版本)

ansible_connection
      與主機的連接類型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默認使用 paramiko.1.2 以後默認使用 'smart','smart' 方式會根據是否支持 ControlPersist, 來判斷'ssh' 方式是否可行.

ansible_ssh_private_key_file
      ssh 使用的私鑰文件.適用於有多個密鑰,而你不想使用 SSH 代理的情況.

ansible_shell_type
      目標系統的shell類型.默認情況下,命令的執行使用 'sh' 語法,可設置爲 'csh''fish'.

ansible_python_interpreter
      目標主機的 python 路徑.適用於的情況: 系統中有多個 Python, 或者命令路徑不是"/usr/bin/python",比如  \*BSD, 或者 /usr/bin/python
      不是 2.X 版本的 Python.我們不使用 "/usr/bin/env" 機制,因爲這要求遠程用戶的路徑設置正確,且要求 "python" 可執行程序名不可爲 python以外的名字(實際有可能名爲python26).

      與 ansible_python_interpreter 的工作方式相同,可設定如 ruby 或 perl 的路徑....

一個主機文件例子:

some_host         ansible_ssh_port=2222     ansible_ssh_user=manager
aws_host          ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host      ansible_python_interpreter=/usr/local/bin/python
ruby_module_host  ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
發佈了49 篇原創文章 · 獲贊 12 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章