facts
Facts通過訪問遠程系統獲取相應的信息。
一個例子就是遠程主機的IP地址或者操作系統是什么. 使用以下命令可以查看哪些信息是可用的:
ansible hostname -m setup
這會返回巨量的變量數據,比如對於Ubutu 12.04系統,Ansible 1.4獲取的信息顯示如下:
"ansible_all_ipv4_addresses": [ "REDACTED IP ADDRESS" ], "ansible_all_ipv6_addresses": [ "REDACTED IPV6 ADDRESS" ], "ansible_architecture": "x86_64", "ansible_bios_date": "09/20/2012", "ansible_bios_version": "6.00", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-3.5.0-23-generic", "quiet": true, "ro": true, "root": "UUID=4195bff4-e157-4e41-8701-e93f0aec9e22", "splash": true }, "ansible_date_time": { "date": "2013-10-02", "day": "02", "epoch": "1380756810", "hour": "19", "iso8601": "2013-10-02T23:33:30Z", "iso8601_micro": "2013-10-02T23:33:30.036070Z", "minute": "33", "month": "10", "second": "30", "time": "19:33:30", "tz": "EDT", "year": "2013" }, "ansible_default_ipv4": { "address": "REDACTED", "alias": "eth0", "gateway": "REDACTED", "interface": "eth0", "macaddress": "REDACTED", "mtu": 1500, "netmask": "255.255.255.0", "network": "REDACTED", "type": "ether" }, "ansible_default_ipv6": {}, "ansible_devices": { "fd0": { "holders": [], "host": "", "model": null, "partitions": {}, "removable": "1", "rotational": "1", "scheduler_mode": "deadline", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null }, "sda": { "holders": [], "host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)", "model": "VMware Virtual S", "partitions": { "sda1": { "sectors": "39843840", "sectorsize": 512, "size": "19.00 GB", "start": "2048" }, "sda2": { "sectors": "2", "sectorsize": 512, "size": "1.00 KB", "start": "39847934" }, "sda5": { "sectors": "2093056", "sectorsize": 512, "size": "1022.00 MB", "start": "39847936" } }, "removable": "0", "rotational": "1", "scheduler_mode": "deadline", "sectors": "41943040", "sectorsize": "512", "size": "20.00 GB", "support_discard": "0", "vendor": "VMware," }, "sr0": { "holders": [], "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)", "model": "VMware IDE CDR10", "partitions": {}, "removable": "1", "rotational": "1", "scheduler_mode": "deadline", "sectors": "2097151", "sectorsize": "512", "size": "1024.00 MB", "support_discard": "0", "vendor": "NECVMWar" } }, "ansible_distribution": "Ubuntu", "ansible_distribution_release": "precise", "ansible_distribution_version": "12.04", "ansible_domain": "", "ansible_env": { "COLORTERM": "gnome-terminal", "DISPLAY": ":0", "HOME": "/home/mdehaan", "LANG": "C", "LESSCLOSE": "/usr/bin/lesspipe %s %s", "LESSOPEN": "| /usr/bin/lesspipe %s", "LOGNAME": "root", "LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:", "MAIL": "/var/mail/root", "OLDPWD": "/root/ansible/docsite", "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "PWD": "/root/ansible", "SHELL": "/bin/bash", "SHLVL": "1", "SUDO_COMMAND": "/bin/bash", "SUDO_GID": "1000", "SUDO_UID": "1000", "SUDO_USER": "mdehaan", "TERM": "xterm", "USER": "root", "USERNAME": "root", "XAUTHORITY": "/home/mdehaan/.Xauthority", "_": "/usr/local/bin/ansible" }, "ansible_eth0": { "active": true, "device": "eth0", "ipv4": { "address": "REDACTED", "netmask": "255.255.255.0", "network": "REDACTED" }, "ipv6": [ { "address": "REDACTED", "prefix": "64", "scope": "link" } ], "macaddress": "REDACTED", "module": "e1000", "mtu": 1500, "type": "ether" }, "ansible_form_factor": "Other", "ansible_fqdn": "ubuntu2.example.com", "ansible_hostname": "ubuntu2", "ansible_interfaces": [ "lo", "eth0" ], "ansible_kernel": "3.5.0-23-generic", "ansible_lo": { "active": true, "device": "lo", "ipv4": { "address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0" }, "ipv6": [ { "address": "::1", "prefix": "128", "scope": "host" } ], "mtu": 16436, "type": "loopback" }, "ansible_lsb": { "codename": "precise", "description": "Ubuntu 12.04.2 LTS", "id": "Ubuntu", "major_release": "12", "release": "12.04" }, "ansible_machine": "x86_64", "ansible_memfree_mb": 74, "ansible_memtotal_mb": 991, "ansible_mounts": [ { "device": "/dev/sda1", "fstype": "ext4", "mount": "/", "options": "rw,errors=remount-ro", "size_available": 15032406016, "size_total": 20079898624 } ], "ansible_nodename": "ubuntu2.example.com", "ansible_os_family": "Debian", "ansible_pkg_mgr": "apt", "ansible_processor": [ "Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz" ], "ansible_processor_cores": 1, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 1, "ansible_processor_vcpus": 1, "ansible_product_name": "VMware Virtual Platform", "ansible_product_serial": "REDACTED", "ansible_product_uuid": "REDACTED", "ansible_product_version": "None", "ansible_python_version": "2.7.3", "ansible_selinux": false, "ansible_ssh_host_key_dsa_public": "REDACTED KEY VALUE" "ansible_ssh_host_key_ecdsa_public": "REDACTED KEY VALUE" "ansible_ssh_host_key_rsa_public": "REDACTED KEY VALUE" "ansible_swapfree_mb": 665, "ansible_swaptotal_mb": 1021, "ansible_system": "Linux", "ansible_system_vendor": "VMware, Inc.", "ansible_user_id": "root", "ansible_userspace_architecture": "x86_64", "ansible_userspace_bits": "64", "ansible_virtualization_role": "guest", "ansible_virtualization_type": "VMware"
引用返回的fact變量:
{{ ansible_devices.sda.model }} #第一個硬盤的model
{{ ansible_hostname } #ansible 遠程主機名
在模板和條件判斷中會經常使用facts。
關閉facts
如果你不需要使用你主機的任何fact數據,你已經知道了你系統的一切,那么你可以關閉fact數據的獲取。
- hosts: whatever
gather_facts: false
本地facts
ansible facts主要是用於獲取遠程系統的數據,從而可以在playbook中作為變量使用。
通常facts中的數據是由Ansible中的 ‘setup’模塊自動發現的.用戶也可以自定義facts模塊。
如果遠程受管理的機器有一個 “/etc/ansible/facts.d” 目錄,那么在該目錄中任何以 ”.fact”結尾的文件都可以在Ansible中提供局部facts.
這些文件可以是JSON,INI或者任何可以返回JSON的可執行文件.
例如建設有一個 /etc/ansible/facts.d/perferences.fact文件:
[general] asdf=1 bar=2
這將產生一個名為 “general” 的哈希表fact,里面成員有 ‘asdf’ 和 ‘bar’. 可以這樣驗證:
ansible <hostname> -m setup -a "filter=ansible_local"
然后你會看到有以下fact被添加:
"ansible_local": { "preferences": { "general": { "asdf" : "1", "bar" : "2" } } }
而且也可以在template或palybook中訪問該數據:
{{ ansible_local.preferences.general.asdf }}
本地命名空間放置其它用戶提供的fact或者playbook中定義的變量覆蓋系統facts值。
如果你有個一個playook,它復制了一個自定義的fact,然后運行它,請顯式調用來重新運行setup模塊,
這樣可以讓我們在該playbook中使用這些fact.否則,在下一個play中才能獲取這些自定義的fact信息.這里有一個示例:
- hosts: webservers tasks: - name: create directory for ansible custom facts file: state=directory recurse=yes path=/etc/ansible/facts.d - name: install custom impi fact copy: src=ipmi.fact dest=/etc/ansible/facts.d - name: re-read facts after adding custom fact setup: filter=ansible_local
然而在該模式中你也可以編寫一個fact模塊,這只不過是多了一個選項。
fact緩存
從一個服務器引用另一個服務器的變量是可行的,比如:
{{ hostvars['asdf.example.com']['ansible_os_family'] }}
如果禁用 “Fact Caching”,為了實現以上功能,Ansible在當前play之前已經與 ‘asdf.example.com’ 通訊過,或者在playbook有其它優先的play.這是ansible的默認配置.
為了避免這些,Ansible 1.8以后版本允許在playbook運行期間保存facts.但該功能需要手動開啟.這有什么用處那?
想象一下,如果我們有一個非常大的基礎設施,里面有數千個主機.Fact緩存可以配置在夜間運行,
但小型服務器集群可以配置fact隨時運行,或者在白天定期運行.即使開啟了fact緩存,也不需要訪問所有服務器來引用它們的變量和信息.
使用fact緩存可以跨群組訪問變量,即使群組間在當前/user/bin/ansible-playbook執行中並沒有通訊過.
為了啟用fact緩存,在大多數plays中你可以修改 ‘gathering’ 設置為 ‘smart’ 或者 ‘explicit’,也可以設置 ‘gather_facts’ 為False.
當前,Ansible可以使用兩種持久的緩存插件: redis和jsonfile.
可以在ansible.cfg中配置fact緩存使用redis:
[defaults] gathering = smart fact_caching = redis fact_caching_timeout = 86400 # seconds
請執行適當的系統命令來啟動和運行redis:
yum install redis service redis start pip install redis
請注意可以使用pip來安裝Python redis庫,在EPEL中的包版本對Ansible來說太舊了.
在當前Ansible版本中,該功能還處於試用狀態,Redis插件還不支持端口或密碼配置,以后會改善這點.
在ansible.cfg中使用以下代碼來配置fact緩存使用jsonfile:
[defaults] gathering = smart fact_caching = jsonfile fact_caching_connection = /path/to/cachedir fact_caching_timeout = 86400 # seconds
fact_caching_connection 是一個放置在可讀目錄(如果目錄不存在,ansible會試圖創建它)中的本地文件路徑。