在Ansible中,可以使用"set_fact"模块来设置事实。下面是一个示例代码:
- name: Set facts silently
hosts: localhost
gather_facts: false # 禁用自动收集事实
tasks:
- name: Set a fact silently
set_fact:
my_fact: "This is my fact"
silent: true # 静默设置事实
- name: Debug the fact
debug:
var: my_fact
在上面的示例中,我们使用"set_fact"模块设置了一个名为"my_fact"的事实,并将其值设置为"This is my fact"。通过将"silent"参数设置为true,我们可以在设置事实时将其静默执行。最后,我们使用"debug"模块打印出该事实的值。
运行上述代码后,输出将会是:
ok: [localhost] => {
"my_fact": "This is my fact"
}
这表示我们成功地设置了一个名为"my_fact"的事实,并将其值打印出来。