Inventory 是 Ansible 管理主機信息的配置文件,至關於系統 HOSTS 文件的功能,默認存放在 /etc/ansible/hosts。
爲方便批量管理主機,便捷使用其中的主機分組,Ansible 經過 Inventory 來定義其主機和組,在使用時經過一1 或 --inventory-file 指定讀取
若是隻有一個 Inventory 時可不用指定路徑,默認讀取 /etc/ansible/hosts。 Inventory 能夠同時存在多個,並且支持動態生成
與 Ansible 命令結合使用時組合以下:php
[root@ansible ~]# ansible -i /etc/ansible/hosts webs -m ping
Inventory配置文件遵循的是INI文件風格,中括號表示組名,其支持將同一個主機加入到不一樣的組中,此外若主機沒有使用默認的SSH的22端口,還能夠在主機名字或者IP後面加上冒號來指定,#號爲註釋行node
示例(編輯/etc/ansible/hosts文件):python
# 直接跟主機IP及其餘端口 192.168.192.129 192.168.192.129:2333 # 使用主機hostname及其餘端口 ansible-node1 ansible-node1:2333 # 主機分組 [webserver] 192.168.192.129 ansible-node1:2333 # 連續主機 [dbserver] 192.168.192.[100:200] # 表示192.168.192.100--192.168.192.200之間的全部主機 ansible-node[10:20]:2333 # 表示ansible-node10:2333--ansible-node20:2333之間的全部主機
在工做中,一般會遇到非標準化的需求配置,考慮安全的問題,一般會把企業內部的80端口修改成其餘的端口,這個就能夠在Inventory中定義,而後在後續的playbook使用
示例(編輯/etc/ansible/hosts文件):nginx
[dbserver] # 自定義http_port的端口爲80,配置maxRequestsPerChild(最大請求數)爲801 192.168.192.129 http_port=808 maxRequestsPerChild=801 # 自定義http_port的端口爲303,配置maxRequestsPerChild(最大請求數)爲909 ansible-node1 http_port=303 maxRequestsPerChild=909
Ansible支持定義組的變量,主要是針對大量的機器的變量定義需求,賦予指定組內全部主機在playbook中可用的變量,等同於逐一給該組下的全部主機賦予同一個變量
示例(編輯/etc/ansible/hosts文件):web
[groupserver] 192.168.192.129 ansible-node1 [groupserver:vars] # 定義groupserver組中全部主機ntp_server的值爲ntp1.aliyun.com ntp_server=ntp1.aliyun.com # 定義groupserver組中全部主機nfs_server的值爲nfs.aliyun.com nfs_server=nfs.aliyun.com
Inventory中,組還能夠包含其餘的組(嵌套),而且也能夠向組中的主機指定變量,不過這些變量只能在playbook中使用,在ansible中不支持,組與組之間能夠相互調用,而且能夠向組中的主機指定變量
示例(編輯/etc/ansible/hosts文件):shell
[apache] 192.168.192.129 [nginx] nginx1.brian.com # 將要嵌套的組名放到一個新的組中 [webserver:children] apache nginx # 給新的組定義ntp_server的變量給主機使用 [webserver:vars] ntp_server=ntp1.aliyun.com
變量除了能夠在Inventory中一併定義,也能夠獨立於Inventory文件以外單獨存儲到YAML格式的配置文件中,這些文件一般以 .yml、.yam!、.json爲後綴或者無後綴。
變量一般從以下4個位置檢索:apache
假如foosball主機同屬於raleigh和webservers組, 那麼其變量在以下文件中設置均有效
示例:
假設你有一些主機,屬於不一樣的數據中心,並依次進行劃分.每個數據中心使用一些不一樣的服務器好比ntp服務器,database服務器等等.
那麼'raleigh'這個組的組變量定義在文件'/etc/ansible/group_vars/raleigh'之中,可能相似這樣json
--- ntp_server: acme.example.org database_server: storage.example.org
經過設置下面的參數,能夠控制 ansible 與遠程主機的交互方式安全
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_ssh_exe # 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" 與 ansible_python_interpreter 的工做方式相同,可設定如 ruby 或 perl 的路徑 示例(編輯/etc/ansible/hosts文件): [testserver] 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 <pattern_goes_here> -m <module_name> -a <arguments> 示例: 1) all(全量)匹配 # all和*相同,但*號要引發來 ansible all -m ping ansible '*' -m ping 2)邏輯或(or)匹配 # 若是要同時對多臺主機或者多個組同時執行,使用冒號分隔 ansible 'db:web' -m ping 3)邏輯非(!)匹配 # 全部在web組但不在db組中的主機 ansible 'web:!db' -m ping 4)邏輯與(&)匹配 # web組和db組同時存在的主機 ansible 'web:&db' -m ping 5)多條件組合 # web和db兩個組中的全部主機在python組中存在且不在php組中的主機 ansible 'web:db:&python:!php' -m ping 6)模糊匹配 # 全部以.python結尾的主機 ansible '*.python' -m ping # 以brian開頭.python結尾的全部主機和db組中的全部主機 ansible 'brian*.python:db' -m ping 7)域切割(python中的列表切割) 主機清單: [web] web1 web2 web3 使用: ansible 'web[0]' -m ping 等於 ansible web1 -m ping ansible 'web[-1]' -m ping 等於 ansible web3 -m ping ansible 'web[0:1]' -m ping 等於 ansible web1,web2 -m ping ansible 'web[1:]' -m ping 等於 ansible web2,web3 -m ping 8)正則匹配 ansible 支持完整的正則匹配 ansible '~(web|db).*\.python\.com' -m ping #檢測beta.python.com 、web.python.com 、green.python.com 、befa.python.cn 、web.python.cn、green.python.cn的存活 ansible '~(beta|web|green)\.python\.(com|cn)' -m ping #檢測Inventory 中全部以192.168開頭的服務器存活信息 ansible '~192\.168\.[0-9]\{\2}.[0-9]\{2,}' -m ping