Ansible 五(inventory文件 ?主機清單)
??Ansible 可同時操作屬于一個組的多臺主機,組和主機之間的關系通過 inventory 文件配置. 默認的文件路徑為 /etc/ansible/hosts
??除默認文件外,你還可以同時使用多個 inventory 文件(后面會講到),也可以從動態源,或云上拉取 inventory 配置信息.詳見 動態Inventory ???????????????? ??http://www.ansible.com.cn/docs/intro_dynamic_inventory.html
主機和組
/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組.這時屬于兩個組的變量都可以為這臺主機所用,至于變量的優先級關系將于以后的章節中討論.
如果有主機的SSH端口不是標準的22端口,可在主機名之后加上端口號,用冒號分隔.SSH 配置文件中列出的端口號不會在 paramiko 連接中使用,會在 openssh 連接中使用.
端口號不是默認設置時,可明確的表示為:
badwolf.example.com:5309
假設你有一些靜態IP地址,希望設置一些別名,但不是在系統的 host 文件中設置,又或者你是通過隧道在連接,那么可以設置如下:
jumper?ansible_ssh_port=5555?ansible_ssh_host=192.168.1.50
在這個例子中,通過 “jumper” 別名,會連接 192.168.1.50:5555.記住,這是通過 inventory 文件的特×××設置的變量. 一般而言,這不是設置變量(描述你的系統策略的變量)的最好方式.后面會說到這個問題.
一組相似的 hostname , 可簡寫如下:
[webservers]
www[01:50].example.com
數字的簡寫模式中,01:50 也可寫為 1:50,意義相同.
你還可以定義字母范圍的簡寫模式:
[databases]
db-[a:f].example.com
對于每一個 host,你還可以選擇連接類型和連接用戶名:
[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 使用:
[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
如果你需要存儲一個列表或hash值,或者更喜歡把 host 和 group 的變量分開配置,請看下一節的說明.
分文件定義host和group變量
在 inventory 主文件中保存所有的變量并不是最佳的方式.還可以保存在獨立的文件中,這些獨立文件與 inventory 文件保持關聯. 不同于 inventory 文件(INI 格式),這些獨立文件的格式為 YAML.詳見 YAML 語法http://www.ansible.com.cn/docs/YAMLSyntax.html .
假設 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, 其中分別設置不同的變量:
/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings
‘raleigh’ 組下的所有主機,都可以使用 ‘raleigh’ 組的變量.當變量變得太多時,分文件定義變量更方便我們進行管理和組織. 還有一個方式也可參考,詳見 Ansible Vault http://www.ansible.com.cn/docs/playbooks_vault.html關于組變量的部分. 注意,分文件定義變量的方式只適用于 Ansible 1.4 及以上版本.
Tip: Ansible 1.2 及以上的版本中,group_vars/ 和 host_vars/ 目錄可放在 inventory 目錄下,或是 playbook 目錄下. 如果兩個目錄下都存在,那么 playbook 目錄下的配置會覆蓋 inventory 目錄的配置.
Tip: 把你的 inventory 文件 和 變量 放入 git repo 中,以便跟蹤他們的更新,這是一種非常推薦的方式.
inventory參數的說明
ansible_ssh_host
將要連接的遠程主機名.與你想要設定的主機的別名不同的話,可通過此變量設置.
ansible_ssh_portssh端口號.如果不是默認的端口號,通過此變量設置.
ansible_ssh_user默認的?ssh?用戶名
ansible_ssh_passssh?密碼(這種方式并不安全,我們強烈建議使用?--ask-pass?或?SSH?密鑰)
ansible_sudo_passsudo?密碼(這種方式并不安全,我們強烈建議使用?--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_filessh?使用的私鑰文件.適用于有多個密鑰,而你不想使用?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
轉載于:https://blog.51cto.com/506554897/1954872