Ansible 能夠對你的基礎設施中多個主機系統同時進行操做。經過選擇在Ansible的inventory列出的一部分主機來實現。inventory默認保存在/etc/ansible/hosts中。你能夠經過指定 -i <path> 參數指定使用其餘文件的路徑。html
不只可使用這個默認的配置文件,你也能夠同時指定多個文件,或者從動態的或者雲資源上來去inventory,詳細可查看Dynamic Inventory。Ansible 2.4 引入了inventory插件,使得主機清單文件能夠靈活而且可定製。python
inventory 文件能夠是許多格式中的一種,取決於你有的inventory插件。例如,/etc/ansible/hosts 是一個ini-格式(Ansible默認的)文件,其看起來像下面這樣:git
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
括號中的標題是組名,用來區分不一樣的主機系統,以及決定你在何時、爲了什麼目的操做主機。web
YAML 格式看起來想下面這樣子:docker
all:
hosts:
mail.example.com
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
把一臺主機放到一個以上的組中是能夠的,例如一個服務器能夠是web服務器,也能夠是數據庫服務器。若是你這樣作了,這時屬於兩個組的變量均可覺得這臺主機所用,至於變量的優先級關係將於之後的章節中討論。shell
若是有主機的SSH端口不是標準的22端口,可在主機名以後加上端口號,用冒號分隔。SSH 配置文件中列出的端口號不會在 paramiko 鏈接中使用,會在 openssh 鏈接中使用。數據庫
端口號不是默認設置時,可顯示設置爲:json
badwolf.example.com:5309
假設你有一些靜態IP地址,而且但願設置一些別名,但不是在系統的 host 文件中設置,或者你是經過隧道在鏈接,那麼你能夠經過變量描述主機:ruby
INI格式服務器
jumper ansible_port=5555 ansible_host=192.0.2.50
YAML格式
hosts: jumper: ansible_port: 5555 ansible_host: 192.0.2.50
在上面的例子中,試着使用Ansible主機jumper(不是一個真正主機名)會去鏈接192.0.2.50的5555端口。
注意,這是經過inventory文件來定義特殊變量的特性。一般來講,這不是最好的定義變量的方式(描述你的系統策略的變量)的最好方式。後面會說到這個問題。
若是你正添加一組類似模式的主機,你能夠按照以下方式列出,而不是列出所有:
[webservers] www[01:50].example.com
對於數字模式,頭部0能夠被包含或者移除,如預期的同樣。範圍是多種多樣的。你能夠指定字符範圍:
[databases]
db-[a:f].example.com
注意
Ansible 2.0 中,
ansible_ssh_user
,ansible_ssh_host
, andansible_ssh_port中的 ‘ssh’已通過時了。使用
ansible_user
,ansible_host
, andansible_port。
若是你使用的是2.0以前的版本,你應該使用老版本的Ansible變量(Ansible_ssh_*),短格式變量會忽略,沒有警告,在老版本Ansible中。
你能夠基於每個主機選擇鏈接類型和鏈接用戶名:
[targets] localhost ansible_connection=local other1.example.com ansible_connection=ssh ansible_user=mpdehaan other2.example.com ansible_connection=ssh ansible_user=mdehaan
正如上面說的,在inventory文件中設置這些變量是臨時的方法,後面咱們會討論如何將這些設置保存爲 ‘host_vars’ 目錄中的獨立的文件。
如上面描述的,能夠很容易的定義hosts的變量,這些變量能夠在後面的playbooks中使用:
[atlanta] host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909
變量也能夠一塊兒應用到整個組中:
ini格式
[atlanta] host1 host2 [atlanta:vars] ntp_server=ntp.atlanta.example.com proxy=proxy.atlanta.example.com
YAML格式
atlanta:
hosts:
host1:
host2:
vars:
ntp_server: ntp.atlanta.example.com
proxy: proxy.atlanta.example.com
要了解這是惟一方便的方式,變量一塊兒應用到多個主機的方式。即便你能夠把目標主機分組,在play執行前,變量老是會被扁平化到主機層級。
能夠經過在INI格式中的.children 後綴或者YAML格式中的children entry 來實現組中組。你可使用 :vars
or vars:
:
INI
[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
YAML
all: children: usa: children: southeast: children: atlanta: hosts: host1: host2: raleigh: hosts: host2: host3: vars: some_server: foo.southeast.example.com halon_system_timeout: 30 self_destruct_countdown: 60 escape_pods: 2 northeast: northwest: southwest:
若是你須要存儲列表或者hash數據,你應該使用另一種方法:把 host 和 group 的變量分開配置,請看後續部分的說明。
子組有幾個屬性須要注意:
有倆個默認組: all
and ungrouped
。 all 包含了全部的主機,ungrouped包含了全部沒有被包含僅除all以外的其餘組中。
每個主機至少屬於倆個組。儘管all和ungrouped老是出現,但他們被隱藏而且不出如今group listtings,例如group_names。
這是Ansible更好的實踐:再也不main inventory文件中存儲變量。
除了在inventory文件中存儲變量以外,host和group變量能夠被存儲在單獨的文件,相對於inventory文件的路徑(不是目錄,老是文件)。
變量文件是YAML格式的。合法的文件擴展名爲 ‘.yml’, ‘.yaml’, ‘.json’, 或者沒有擴展名。詳細可查看 YAML Syntax
假設inventory文件是:
/etc/ansible/hosts
若是有一個主機名爲foosball,raleigh和webservers組,在下面位置上的YAML文件中的變量能夠被主機和組中的主機訪問:
/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json' /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組中的主機會擁有這些文件中定義的變量。這是有用的,當單個文件開始變得愈來愈大時。 還有一個方式也可參考,詳見 Ansible Vault 關於組變量的部分。注意,分文件定義變量的方式只適用於 Ansible 1.4 及以上版本。
提示:Ansible 1.2以後,group_vars/ 和 host_vars/ 目錄能夠存在playbook目錄或者inventory目錄。若是倆個路徑都存在,playbook中的變量會覆蓋inventory設置的變量。
提示:保存你的inventory file和變量在一個git repo(或者其餘版本控制)是很是棒的方式,能夠追蹤你的inventory和host變量的變化。
正如上面提到的,設置以下變量控制Ansible如何與遠程主機交互。
主機鏈接參數:
ansible_connection 到遠端主機的鏈接類型。這個能夠是任何一個Ansible鏈接插件的名字。SSH協議類型是:smart/ssh/paramiko。默認是smart。非SSH類型在下一章節描述。
全部鏈接插件共有的通常參數:
ansible_host 要鏈接的主機(IP地址等),它不一樣於你給出的別名
ansible_port 若是不是22,指定ssh端口
ansible_user 默認使用的用戶名
特定鏈接類型的參數
ansible_ssh_pass
要使用的ssh密碼,不要存儲這個變量爲普通文本,老是使用vault。詳細查看 Variables and Vaults
ansible_ssh_private_key_file
指定ssh使用的私鑰文件。若是使用多個keys而且你不像使用SSH代理時有用
ansible_ssh_common_args
該設置老是被添加到 sftp/scp/ssh 默認的命令行當爲某一主機或者組配置一個ProxyCommand命令時有用
ansible_sftp_extra_args
該設置總添加到默認的 sftp 命令行
ansible_scp_extra_args
該設置老是添加到默認的scp命令行
ansible_ssh_extra_args
該設置老是添加到默認的ssh命令行
ansible_ssh_pipelining
決定是否使用SSH pipelining,這個能夠覆寫Ansible.cfg配置文件中的pipelining設置
ansible_ssh_executable (added in version 2.2)
該設置覆寫默認使用系統ssh的行爲,能夠覆寫Ansible.cfg配置文件中的pipelining設置
特權上升
ansible_become
等價於 ansible_sudo 或 ansible_su, 容許強制特權上升
ansible_become_method
容許設置特權上升方法
ansible_become_user
等價於 ansible_sudo_user 或 ansible_su_user, 容許經過特權上升設置你成爲的用戶
ansible_become_pass
等價於 ansible_sudo_pass 或 ansible_su_pass, 容許你設置特權上升密碼
ansible_become_exe
等價於 ansible_sudo_exe 或 ansible_su_exe, 容許你設置選擇的上升方法的可執行程序
ansible_become_flags
等價於 ansible_sudo_flags 或 ansible_su_flags, 容許你設置標誌傳給被選擇的方法,這個一樣能夠全局的設置,在ansible.cfg中
遠程主機環境參數
ansible_shell_type
目的主機的shell type。你不該該使用這個設置除非設置了ansible_shell_executable爲一個非bourne(sh) 兼容shell。
默認命令使用sh-style語法格式化。
設置該參數爲csh或者fish會致使目標系統上指定的命令跟隨那些shell的語法。
ansible_python_interpreter
目的主機的python path。這是有用的對於多餘一個Python,或者python沒有在/usr/bin/python中的系統,來講是有用的
例如*BSD或者其/usr/bin/python不是2.x版本的系統。
咱們不使用 /usr/bin/env 機制,由於它要求遠程用戶來設置正確的path而且須要假設python可執行程序是名爲「python」,而不是命名爲python2.6
ansible_*_interpreter
用於ruby或者perl的東西 而且和 ansible_python_interpreter 同樣工做。 這回替代該臺主機的模塊shebang
ansible_shell_executable 2.1 新特性
這設置了ansible要使用的目的主機上的shell,覆寫ansible.cfg中executable配置。其默認值是/bin/sh。你僅在不能使用/bin/sh時更改它
Ansible-INI host file 例子
some_host ansible_port=2222 ansible_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上執行playbook,可是它不限於該鏈接類型。使用主機鏈接參數,ansible_connection=<connector>,鏈接類型能夠被更改。下面是支持的非SSH鏈接類型:
local
該鏈接器能夠用來部署playbook來控制自身機器。
docker
該鏈接器直接在docker容器中部署playbook,使用本地 docker client,下面的參數能夠傳入該鏈接器處理:
ansible_host
The name of the Docker container to connect to.
ansible_user
The user name to operate within the container. The user must exist inside the container.
ansible_become
If set to true the become_user will be used to operate within the container.
ansible_docker_extra_args
Could be a string with any additional arguments understood by Docker, which are not command specific. This parameter is mainly used to configure a remote Docker daemon to use.
- name: create jenkins container docker_container: docker_host: myserver.net:4243 name: my_jenkins image: jenkins - name: add container to inventory add_host: name: my_jenkins ansible_connection: docker ansible_docker_extra_args: "--tlsverify --tlscacert=/path/to/ca.pem --tlscert=/path/to/client-cert.pem --tlskey=/path/to/client-key.pem -H=tcp://myserver.net:4243" ansible_user: jenkins changed_when: false - name: create directory for ssh keys delegate_to: my_jenkins file: path: "/var/jenkins_home/.ssh/jupiter" state: directory