ansible inventory

一、inventory文件

一、inventory文件說明:

目標主機的組織編排,靈活的將ad-hoc或plays傳遞給不一樣的主機。
主機、plays較少,直接在ansible 根目錄使用hosts文件便可,
若是較爲複雜,能夠建立inventory文件夾,分不一樣inventory文件:
  1. inventory
  2. - hosts01
  3. - hosts02
  4. - test
 inventory文件的編寫例子:
  1. mail.example.com
  2. [webservers]
  3. foo.example.com
  4. bar.example.com
  5. [dbservers]
  6. one.example.com
  7. two.example.com
  8. three.example.com
  9. badwolf.example.com:5309
  10. jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
  11. [webservers]
  12. www[01:50].example.com
  13. [databases]
  14. db-[a:f].example.com
  15. [targets]
  16. localhost ansible_connection=local
  17. other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan
  18. other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan
  19. [atlanta]
  20. host1 http_port=80 maxRequestsPerChild=808
  21. host2 http_port=303 maxRequestsPerChild=909
  22. [atlanta]
  23. host1
  24. host2
  25. [atlanta:vars]
  26. ntp_server=ntp.atlanta.example.com
  27. proxy=proxy.atlanta.example.com
  28. [atlanta]
  29. host1
  30. host2
  31. [raleigh]
  32. host2
  33. host3
  34. [southeast:children]
  35. atlanta
  36. raleigh
  37. [southeast:vars]
  38. some_server=foo.southeast.example.com
  39. halon_system_timeout=30
  40. self_destruct_countdown=60
  41. escape_pods=2
  42. [usa:children]
  43. southeast
  44. northeast
  45. southwest
  46. northwest

二、inventory中的變量

一、inventory文件中定義

在主機以後: key=value
inventory文件例子:
  1. some_host ansible_ssh_port=2222 ansible_ssh_user=manager
  2. aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
  3. freebsd_host ansible_python_interpreter=/usr/local/bin/python
  4. ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
參數說明:
  1. ansible_ssh_host
  2. 將要鏈接的遠程主機名.與你想要設定的主機的別名不一樣的話,可經過此變量設置.
  3. ansible_ssh_port
  4. ssh端口號.若是不是默認的端口號,經過此變量設置.
  5. ansible_ssh_user
  6. 默認的 ssh 用戶名
  7. ansible_ssh_pass
  8. ssh 密碼(這種方式並不安全,咱們強烈建議使用 --ask-pass SSH 密鑰)
  9. ansible_sudo_pass
  10. sudo 密碼(這種方式並不安全,咱們強烈建議使用 --ask-sudo-pass)
  11. ansible_sudo_exe (new in version 1.8)
  12. sudo 命令路徑(適用於1.8及以上版本)
  13. ansible_connection
  14. 與主機的鏈接類型.好比:local, ssh 或者 paramiko. Ansible 1.2 之前默認使用 paramiko.1.2 之後默認使用 'smart','smart' 方式會根據是否支持 ControlPersist, 來判斷'ssh' 方式是否可行.
  15. ansible_ssh_private_key_file
  16. ssh 使用的私鑰文件.適用於有多個密鑰,而你不想使用 SSH 代理的狀況.
  17. ansible_shell_type
  18. 目標系統的shell類型.默認狀況下,命令的執行使用 'sh' 語法,可設置爲 'csh' 'fish'.
  19. ansible_python_interpreter
  20. 目標主機的 python 路徑.適用於的狀況: 系統中有多個 Python, 或者命令路徑不是"/usr/bin/python",好比 \*BSD, 或者 /usr/bin/python
  21. 不是 2.X 版本的 Python.咱們不使用 "/usr/bin/env" 機制,由於這要求遠程用戶的路徑設置正確,且要求 "python" 可執行程序名不可爲 python之外的名字(實際有可能名爲python26).
  22. ansible_python_interpreter 的工做方式相同,可設定如 ruby perl 的路徑....
 

二、引用其餘文件中的變量

在 inventory 主文件中保存全部的變量並非最佳的方式.
還能夠保存在獨立的文件中,這些獨立文件與 inventory 文件保持關聯. 
不一樣於  inventory 文件(INI 格式),這些獨立文件的格式爲 YAML.
例如:
  1. /etc/ansible/group_vars/raleigh
  2. /etc/ansible/group_vars/webservers
  3. /etc/ansible/host_vars/foosball
  4. #/etc/ansible/group_vars/raleigh
  5. ---
  6. ntp_server: acme.example.org
  7. database_server: storage.example.org
當變量變得太多時,分文件定義變量更方便咱們進行管理和組織:
爲一個主機,或一個組,建立一個目錄,目錄名就是主機名或組名.
目錄中的能夠建立多個文件, 文件中的變量都會被讀取爲主機或組的變量.
以下 ‘raleigh’ 組對應於 /etc/ansible/group_vars/raleigh/ 目錄,
其下有兩個文件 db_settings 和 cluster_settings, 其中分別設置不一樣的變量:
  1. /etc/ansible/group_vars/raleigh/db_settings
  2. /etc/ansible/group_vars/raleigh/cluster_settings

二、動態inventory

使用配置管理系統常常有一種需求,可能要在其餘的軟件系統中保存本身的 inventory 配置信息.

Ansible 自己經過基於文本的方式來記錄 inventory 配置信息,這在前面已介紹過(詳見 Inventory文件 ).html

除此以外,Ansible 也支持用其餘方式保存配置信息.python

在其餘軟件系統保存配置信息的例子有:git

 

  1. 1, 從雲端拉取 inventory
  2. 2, LDAPLightweight Directory Access Protocol,輕量級目錄訪問協議)
  3. 3, `Cobbler <http://cobbler.github.com>`_
  4. 4, 或者是一份昂貴的企業版的 CMDB(配置管理數據庫) 軟件.

三、pattern 與 inventory

ad-hoc中 pattern 匹配主機例子:
  1. #命令格式以下:
  2. ansible <pattern_goes_here> -m <module_name> -a <arguments>
  3. ansible webservers -m service -a "name=httpd state=restarted"
  4. #其中的<pattern_goes_here> 還能夠有如下寫法匹配
  5. #全部主機
  6. all
  7. *
  8. #ip,hostname
  9. one.example.com
  10. one.example.com:two.example.com
  11. 192.168.1.50
  12. 192.168.1.*
  13. #組
  14. webservers
  15. webservers:dbservers
  16. #非
  17. webservers:!phoenix
  18. #交集
  19. webservers:dbservers:&staging:!phoenix
  20. #傳參數,使用變量
  21. webservers:!{{excluded}}:&{{required}}
  22. #單個host names, IPs , groups的通配
  23. *.example.com
  24. *.com
  25. #host和group混合使用
  26. one*.com:dbservers
  27. #匹配組中的某些server
  28. webservers[0]
  29. webservers[0-25]
  30. #正則匹配
  31. ~(web|db).*\.example\.com
pattern 的一些技能:
適用於ansible和ansible-playbook
  1. #經過 --limit 標記來添加排除條件
  2. ansible-playbook site.yml --limit datacenter2
  3. #從文件讀取hosts,文件名以@爲前綴
  4. ansible-playbook site.yml --limit @retry_hosts.txt
相關文章
相關標籤/搜索