什么是 facts

Ansible facts are variables that are automatically discovered by Ansible on a managed hosts. 说白了就是内置变量。

我们可以使用 debug 模块来打印 ansible_facts 变量的值来观察 ansible facts。

---
- name: print ansible facts
  hosts: web
  tasks:
  - name: print the value of ansible_facts
    debug:
      var: ansible_facts
      #var: ansible_facts['all_ipv4_addresses']

Ansible 2.5 之前,facts 是以 ansible_ 开头的单独的变量,而不是 ansible_facts 变量的一部分。

# cat demo.yml 
---
- name: print ansible facts
  hosts: web
  tasks:
  - name: print the value of ansible_facts
    debug:
      var: ansible_all_ipv4_addresses        

可以使用 setup 模块来查看这些单独的变量。

# ansible web -m setup
# ansible demogrp -m setup -a 'filter=ansible_architecture'
# ansible demogrp -m setup -a 'filter=*architecture*'    

建议使用新的语法,如果想关闭旧的命令系统支持,可以通过配置 ansible.cfg 来实现:

[defaults]
inject_facts_as_vars = False

注意:这个只是关闭 playbook 对变量旧的命令系统的支持,不影响 ad hoc 命令调用 setup 模块及显示。

禁用 facts 收集

---
- name: print ansible facts
  hosts: web
  gather_facts: no
  tasks:
  - name: you can manually gather facts any tim by setup
    setup:    # 尽管这儿是调用的 setup 模块,但是是新命名系统显示
  - name: print the value of ansible_facts
    debug:
      var: "ansible_facts"

自定义 facts

自定义 facts 是定义在被管理主机的 /etc/ansible/facts.d/*.fact 文件中。
文件内容可以按照 INI 格式写,也可以按 JSON 格式书写。

INI 格式示例:

# cat /etc/ansible/facts.d/ini.fact 
[packages]
web_package=httpd
db_package=mariadb-server

[users]
webuser=apache
dbuser=mysql

JSON 格式示例:

# cat /etc/ansible/facts.d/json.fact
{
  "packages": {
     "web_package": "httpd",
     "db_package": "mariadb-server"
  },
  "users": {
     "webuser": "apache",
     "dbuser": "mysql"
  }
}

自定义 facts 由 setup 模块保存在 ansible_facts.ansible_local 变量中:

# ansible web -m setup -a 'filter=*local*'

servera.lab.example.com | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "ini": {
                "packages": {
                    "db_package": "mariadb-server",
                    "web_package": "httpd"
                },
                "users": {
                    "dbuser": "mysql",
                    "webuser": "apache"
                }
            },
            "json": {
                "packages": {
                    "db_package": "mariadb-server",
                    "web_package": "httpd"
                },
                "users": {
                    "dbuser": "mysql",
                    "webuser": "apache"
                }
            }
        },
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
} 

其调用方法跟内置变量一样:

# cat demo.yml 
---
- name: print ansible facts
  hosts: web
  tasks:
  - name: print the value of ansible_facts
    debug:
      var: ansible_facts.ansible_local.ini.packages.db_package

USING MAGIC VARIABLES

magic variables 不是 facts,但也是由 ansible 自动设置的变量。
四个用得最多最重要的 magic variables 是:
hostvars:包含了被管理主机的 ansible 相关的变量,如果采集了 facts,也包含了 facts 变量。
group_names:列出被管理主机所在的所有组
groups:列出资产清单中的所有组和主机
inventory_hostname/inventory_hostname_short:列出被管理主机在资产清单中配置的主机名,当禁用了 facts 收集或者不想依赖 facts 报告的主机名时用(这儿报告的主机名跟 facts 报告的主机名可能不一致,至于为什么不一致,教材上也没说,只写了个 various reasons)

使用案例:对所有主机执行剧本,但只在 servera 上安装软件

# cat demo.yml 
---
- name: print ansible facts
  hosts: allhosts
  gather_facts: no
  tasks:
  - name: install httpd on servera
    yum:
      name: httpd
      state: installed
    when: inventory_hostname_short == "servera"
-- By 许望(RHCA、OCM、VCP)
最后修改:2020 年 05 月 07 日 09 : 53 AM
如果觉得我的文章对你有用,请随意赞赏