Ansible 可同時操作屬于一個組的多臺主機,組和主機之間的關系通過 inventory 文件配置. 默認的文件路徑為 /etc/ansible/hosts
主機清單示例
mail.example.com # FQDN
[webservers] # 方括號[]中是組名
host1
host2:5522 # 指定連接主機得端口號
localhost ansible_connection=local # 定義連接類型
host3 http_port=80 maxRequestsPerChild=808 # 定義主機變量
host4 ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50 # 定義主機ssh連接端口和連接地址
www[1:50].example.com # 定義 1-50范圍內的主機
www-[a:f].example.com # 定義a-f范圍內內的主機
[dbservers]
three.example.com ansible_python_interpreter=/usr/local/bin/python #定義python執行文件
192.168.77.123 ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3 # 定義ruby執行文件
[webservers:vars] # 定義webservers組的變量
ntp_server= ntp.example.com
proxy=proxy.example.com
[server:children] # 定義server組的子成員
webservers
dbservers
[server:vars] # 定義server組的變量
zabbix_server:192.168.77.121
Inventory 參數的說明
主機連接:
參數 | 說明 |
---|---|
ansible_connection | 與主機的連接類型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默認使用 paramiko.1.2 以后默認使用 'smart','smart' 方式會根據是否支持 ControlPersist, 來判斷'ssh' 方式是否可行. |
ssh連接參數:
參數 | 說明 |
---|---|
ansible_ssh_host | 將要連接的遠程主機名.與你想要設定的主機的別名不同的話,可通過此變量設置. |
ansible_ssh_port | ssh端口號.如果不是默認的端口號,通過此變量設置. |
ansible_ssh_user | 默認的 ssh 用戶名 |
ansible_ssh_pass | ssh 密碼(這種方式并不安全,我們強烈建議使用 --ask-pass 或 SSH 密鑰) |
ansible_ssh_private_key_file | ssh 使用的私鑰文件.適用于有多個密鑰,而你不想使用 SSH 代理的情況. |
ansible_ssh_common_args | 此設置附加到sftp,scp和ssh的缺省命令行 |
ansible_sftp_extra_args | 此設置附加到默認sftp命令行。 |
ansible_scp_extra_args | 此設置附加到默認scp命令行。 |
ansible_ssh_extra_args | 此設置附加到默認ssh命令行。 |
ansible_ssh_pipelining | 確定是否使用SSH管道。 這可以覆蓋ansible.cfg中得設置。 |
遠程主機環境參數:
參數 | 說明 |
---|---|
ansible_shell_type | 目標系統的shell類型.默認情況下,命令的執行使用 'sh' 語法,可設置為 'csh' 或 'fish'. |
ansible_python_interpreter | 目標主機的 python 路徑.適用于的情況: 系統中有多個 Python, 或者命令路徑不是"/usr/bin/python",比如 *BSD, 或者 /usr/bin/python |
ansible_*_interpreter | 這里的"*"可以是ruby 或perl 或其他語言的解釋器,作用和ansible_python_interpreter 類似 |
ansible_shell_executable | 這將設置ansible控制器將在目標機器上使用的shell,覆蓋ansible.cfg中的配置,默認為/bin/sh。 |
動態主機清單示例
inventory.py
#!/usr/bin/env python
'''
Example custom dynamic inventory script for Ansible, in Python.
'''
import os
import sys
import argparse
try:
import json
except ImportError:
import simplejson as json
class ExampleInventory(object):
def __init__(self):
self.inventory = {}
self.read_cli_args()
# Called with `--list`.
if self.args.list:
self.inventory = self.example_inventory()
# Called with `--host [hostname]`.
elif self.args.host:
# Not implemented, since we return _meta info `--list`.
self.inventory = self.empty_inventory()
# If no groups or vars are present, return empty inventory.
else:
self.inventory = self.empty_inventory()
print json.dumps(self.inventory);
# Example inventory for testing.
def example_inventory(self):
return {
'group': {
'hosts': ['192.168.28.71', '192.168.28.72'],
'vars': {
'ansible_ssh_user': 'vagrant',
'ansible_ssh_private_key_file':
'~/.vagrant.d/insecure_private_key',
'example_variable': 'value'
}
},
'_meta': {
'hostvars': {
'192.168.28.71': {
'host_specific_var': 'foo'
},
'192.168.28.72': {
'host_specific_var': 'bar'
}
}
}
}
# Empty inventory for testing.
def empty_inventory(self):
return {'_meta': {'hostvars': {}}}
# Read the command line args passed to the script.
def read_cli_args(self):
parser = argparse.ArgumentParser()
parser.add_argument('--list', action = 'store_true')
parser.add_argument('--host', action = 'store')
self.args = parser.parse_args()
# Get the inventory.
ExampleInventory()
$ ./inventory.py --list
{"group": {"hosts": ["192.168.28.71", "192.168.28.72"], "vars":{"ansible_ssh_user":
"vagrant","ansible_ssh_private_key_file":"~/.vagrant.d/insecure_private_key", "example_variable": "value"}},
"_meta": {"hostvars": {"192.168.28.72": {"host_specific_var": "bar"}, "192.168.28.71": {"host_specific_var": "foo"}}}}
$ ansible all -i inventory.py -m ping
$ ansible all -i inventory.py -m debug -a "var=host_specific_var"
動態主機清單見 Ansible 開發插件之【動態主機清單】。
更多文章請看 Ansible 專題文章總覽