Ansible 事实变量和魔法变量

Ansible 事实变量和魔法变量

事实变量

顾名思义事实变量就是根据事实定义的变量。比方说被控主机的配置信息,如 cpu、内存、硬盘、IP 和系统版本等信息。

默认的事实变量

ansible_facts 是 Ansible 的事实变量。

ansible localhost -m ansible.builtin.setup

通过 ansible.builtin.setup 可以查看受控主机的事实变量。

ansible localhost -m ansible.builtin.setup -a 'filter=ansible_distribution'
ansible localhost -m ansible.builtin.setup -a 'filter=ansible_distribution_file_variety'
ansible localhost -m ansible.builtin.setup -a 'filter=ansible_distribution_major_version'
ansible localhost -m ansible.builtin.setup -a 'filter=ansible_distribution_version'

通过 -a 'filter=ansible_distribution' 来进行过滤,所有 ansible.builtin.setup 模块看到的变量都可以引用。

ansible.builtin.setup 模块的 filter 参数只能做一层过滤,想要获取更细的过滤需要使用 ansible.builtin.debug 模块。

举个例子:

[root@awx-1 ansible]# ansible localhost -m ansible.builtin.setup -a 'filter=ansible_python'
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_python": {
            "executable": "/usr/bin/python3.11",
            "has_sslcontext": true,
            "type": "cpython",
            "version": {
                "major": 3,
                "micro": 5,
                "minor": 11,
                "releaselevel": "final",
                "serial": 0
            },
            "version_info": [
                3,
                11,
                5,
                "final",
                0
            ]
        }
    },
    "changed": false
}
[root@awx-1 ansible]# ansible localhost -m ansible.builtin.setup -a 'filter=ansible_python.version'
localhost | SUCCESS => {
    "ansible_facts": {},
    "changed": false
}
[root@awx-1 ansible]# ansible localhost -m ansible.builtin.setup -a 'filter=ansible_python.version'
localhost | SUCCESS => {
    "ansible_facts": {},
    "changed": false
}
[root@awx-1 ansible]# ansible localhost -m ansible.builtin.debug -a 'var=ansible_python.version'
localhost | SUCCESS => {
    "ansible_python.version": {
        "major": 3,
        "micro": 5,
        "minor": 11,
        "releaselevel": "final",
        "serial": 0
    }
}

自定义事实变量

cat /etc/ansible/facts.d/install.fact
[install]
install_date=2024-8-4

cat /etc/ansible/facts.d/uptime.fact
{
  "uptime": {
    "time": "10d",
    "health": "true"
  }
}

在受控主机创建 /etc/ansible/facts.d/,在 /etc/ansible/facts.d/ 目录下创建变量文件(必须以 .fact 结尾),如 /etc/ansible/facts.d/install.fact,文件内容可以是 INI 格式,也可以是 JSON 格式。

ansible localhost -m setup -a 'filter=ansible_local'
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "install_vars": {
                "install": {
                    "install_date": "2024-8-4"
                }
            },
            "uptime_vars": {
                "uptime": {
                    "health": "true",
                    "time": "10d"
                }
            }
        }
    },
    "changed": false
}

通过 -a 'filter=ansible_local' 可以查找自定义事实变量,ansible_local 表示自定义事实变量,install_vars 和自定义事实变量文件名对应,install 对应文件里的 [install]"install_date": "2024-8-4" 对应文件里的 install_date=2024-8-4

ansible localhost -m ansible.builtin.debug -a 'msg="{{ ansible_local.uptime_vars.uptime.time }}"'
ansible localhost -m ansible.builtin.debug -a 'var=ansible_local.uptime_vars.uptime.time'

打印变量的时候使用 ansible.builtin.debug 模块打印比较方便.

魔法变量

https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#magic-variables

Ansible 魔法变量是反映当前的一些状态的变量,如配置文件位置、Ansible 版本、Python 版本、当前主机在主机清单中的名字和所属主机组,这些变量的值都是随着配置文件或主机清单等来确定的,可以用 ansible all -m debug -a 'var=hostvars' 来查看所有魔法变量。

#查看所有魔法变量
ansible all -m debug -a 'var=hostvars'

#查看主机清单中设置的主机名
ansible all -m debug -a 'var=inventory_hostname'

#查看主机所属组
ansible all -m debug -a 'var=group_names'

上边列出几个查看的例子,其他的以此类推。

魔法变量和事实变量的区别

我自己的理解,事实变量记录着当前受控主机的相关信息,比如 CPU、内存、硬盘,当前主机的 python 版本,这些变量都是从受控主机采集的信息,所以它们的值会随着受控主机的改变而改变,Ansible 是没有办法在 server2 主机上使用 server1 的事实变量的。

但是魔法变量不一样,如受控主机在主机清单的名字、主机组名字、Ansible 的配置文件、控制节点的 python 路径等,这些都是受 Ansible 配置文件或主机清单来配置的,因为在 Ansible 配置文件和主机清单确定之后,魔法变量的值也都确定了,所以是可以在 server2 上使用 server1 的魔法变量的,例如:

ansible server1,server2 -m debug -a 'var=hostvars.server1.inventory_hostname'

这个命令会在 server1 和 server2 都输出 server1。

再举一个例子:

[root@awx-1 ansible]# cat hosts
servera ansible_ssh_host=127.0.0.1 ansible_ssh_user=root ansible_ssh_password=redhat
[root@awx-1 ansible]# ansible servera -i hosts -m ansible.builtin.debug -a 'var=hostvars.servera.inventory_hostname'
servera | SUCCESS => {
    "hostvars.servera.inventory_hostname": "servera"
}
[root@awx-1 ansible]# ansible servera -i hosts -m ansible.builtin.debug -a 'var=ansible_hostname'
servera | SUCCESS => {
    "ansible_hostname": "awx-1"
}

主机清单里有 servera 主机,通过 servera 主机查看魔法变量就能看到 servera,这个变量就是通过主机清单定义的,跟被控主机的配置没有任何关系。

Ansible 事实变量和魔法变量
https://www.linuxstudynotes.com/2025/04/23/ansible/ansible-%e4%ba%8b%e5%ae%9e%e5%8f%98%e9%87%8f%e5%92%8c%e9%ad%94%e6%b3%95%e5%8f%98%e9%87%8f/
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇