Ansible inventory 文件

ansible.cfg 中的 inventory 參數定義主機的列表,默認存放在 /etc/ansible/hosts。除此配置文件外,也能夠同時使用多個 inventory 文件,或者從動態雲資源拉取 inventory 配置信息,支持不一樣的格式,如 yamlini 等。python

在本機 ubuntu 18.04 操做其餘三主機:git

  • localhost
  • 10.53.141.252ubuntu 18.04,用戶 cec
  • 10.53.128.20RHEL 5.8,用戶 durant,安裝多版本 Python

配置好 ssh keysweb

主機

INIshell

localhost ansible_connection=local
10.53.141.252 ansible_user=cec
10.53.128.20 ansible_user=durant ansible_python_interpreter="/usr/bin/env python3"
複製代碼

YAMLall 也可以使用其餘名字json

all: 
  hosts: 
    localhost: 
      ansible_connection: local
    10.53.141.252: 
      ansible_user: cec
    10.53.128.20: 
      ansible_user: durant
      ansible_python_interpreter: "/usr/bin/env python3"
複製代碼

INIubuntu

localhost ansible_connection=local

[dev]
10.53.141.252 ansible_user=cec
10.53.128.20 ansible_user=durant ansible_python_interpreter="/usr/bin/env python3"
複製代碼

YAMLbash

all: 
  hosts: 
    localhost: 
      ansible_connection: local
  children:
    dev: 
      hosts: 
        10.53.141.252: 
          ansible_user: cec
        10.53.128.20: 
          ansible_user: durant
          ansible_python_interpreter: "/usr/bin/env python3"
複製代碼

在上面的例子中,包含三個組:allungroupeddevssh

  • all:全部的主機
  • ungrouped:不在除 all 以外的其餘的組的主機

所任何一個主機都至少在兩個組中。即便 allungrouped 一直有,但不會出如今組列表(如group_names)中。ui

主機變量

給主機分配變量,在 playbook 中會用到spa

[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
複製代碼

請注意,這只是個同時給多個主機賦予變量的簡便方法,即便你能夠根據組指定目標主機。variables are always flattened to the host level before a play is executed.

嵌套組和變量

INI 文件使用 :children 或在 YAML 文件中使用 children: 入口,能夠一個組設置爲另外一個組的成員,使用 :varsvars: 設置變量。

[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
複製代碼
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:
複製代碼

子組有下列注意點:

  • 是一個子組的成員的主機,也是父組的成員
  • 子組的變量的優先級別比父組高
  • 組能夠有多個組和子組,但不是環形關係
  • 主機能夠有多個組,但一個主機只會有一個實例,從多個組中合併數據。

YAML 文件中,組的下一層只能是 varschildrenhosts

分離主機、組的特殊數據

若是要存儲序列或 hash 值,或把 host、group 的變量與 inventory 文件分開,也有辦法作到。

Ansible 實踐中也不推薦在 inventory 文件中存儲變量。

將主機、組的變量存放在 inventory 相關的一個獨立文件中(不是目錄,一般是文件),這些變量文件是 YAML 格式,擴展名通常是 ymlyamljson 或者沒有擴展名

若是 inventory 文件路徑是 /etc/ansible/hosts,主機名是 football,在組 raleighwebservers 中。下面的變量文件都是有效:

/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
複製代碼

變量文件內容:

---
ntp_server: acme.example.org
database_server: storage.example.org
複製代碼

固然,這些文件不存在也不要緊,由於是可選特性。

在高級用法中,也能夠在組或主機下繼續建立目錄,Ansible使字典編纂順序讀取這文件,例如 raleigh 組:

/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings
複製代碼

raleigh 組的全部主機都會有這些文件定義的變量,當一個單獨文件太大的時候,用這個方法可更好的組織文件結構,或者當使用 Ansible Vault 做爲組變量的一部分時。

group_vars/host_vars/ 能夠存放在 playbook 或者 inventory 目錄。若是兩個路徑都有,playbook 目錄下的變量會覆蓋 inventory 目錄下的變量。

inventory 文件和變量加入 git 倉庫(或其餘版本控制系統)是一個追蹤 inventory 和主機變量變化的好方法。

變量合併的方式

play 運行前,會對特定的主機進行變量合併,這可使 Ansible 專一於主機和任務,所以組實際只存在於 inventory 和主機匹配中。

下面是變量的優先順序(低 -> 高):

  • all,由於 all 是全部組的父組
  • 父組
  • 子組
  • host

根據字母順序合併相同級別的父組、子組,後面的覆前面的。

Ansible 2.4 開始,可使用 ansible_group_priority 改變相同級別組的合併順序( parent/child 的順序被解析後)。數字越大,越後合併,即優先度更高,默認是 1。

例如,若是這兩個組優先級相同,結果是 testvar == b,但因爲給 a_group 更高的權限,結果是 testvar == a

a_group:
    testvar: a
    ansible_group_priority: 10
b_group:
    testvar: b
複製代碼

Inventory 參數

  • ansible_connection
  • ansible_host
  • ansible_user
  • ansible_ssh_pass
  • ansible_ssh_private_key_file
  • ansible_ssh_common_args
  • ansible_sftp_extra_args
  • ansible_scp_extra_args
  • ansible_ssh_extra_args
  • ansible_ssh_pipelining
  • ansible_ssh_executable
  • ansible_become
  • ansible_become_method
  • ansible_become_user
  • ansible_become_pass
  • ansible_become_exe
  • ansible_become_flags
  • ansible_shell_type
  • ansible_python_interpreter
  • ansible_*_interpreter
  • ansible_shell_executable
相關文章
相關標籤/搜索