inventory
- hosts01
- hosts02
- test
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
badwolf.example.com:5309
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
[webservers]
www[01:50].example.com
[databases]
db-[a:f].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
[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
[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
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
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 的路徑....
/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
#/etc/ansible/group_vars/raleigh
---
ntp_server: acme.example.org
database_server: storage.example.org
/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings
Ansible 自己經過基於文本的方式來記錄 inventory 配置信息,這在前面已介紹過(詳見 Inventory文件 ).html
除此以外,Ansible 也支持用其餘方式保存配置信息.python
在其餘軟件系統保存配置信息的例子有:git
1, 從雲端拉取 inventory
2, LDAP(Lightweight Directory Access Protocol,輕量級目錄訪問協議)
3, `Cobbler <http://cobbler.github.com>`_
4, 或者是一份昂貴的企業版的 CMDB(配置管理數據庫) 軟件.
#命令格式以下:
ansible <pattern_goes_here> -m <module_name> -a <arguments>
ansible webservers -m service -a "name=httpd state=restarted"
#其中的<pattern_goes_here> 還能夠有如下寫法匹配
#全部主機
all
*
#ip,hostname
one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.*
#組
webservers
webservers:dbservers
#非
webservers:!phoenix
#交集
webservers:dbservers:&staging:!phoenix
#傳參數,使用變量
webservers:!{{excluded}}:&{{required}}
#單個host names, IPs , groups的通配
*.example.com
*.com
#host和group混合使用
one*.com:dbservers
#匹配組中的某些server
webservers[0]
webservers[0-25]
#正則匹配
~(web|db).*\.example\.com
#經過 --limit 標記來添加排除條件
ansible-playbook site.yml --limit datacenter2
#從文件讀取hosts,文件名以@爲前綴
ansible-playbook site.yml --limit @retry_hosts.txt