我有像这样在本地主机上运行的 ansible 任务
- name: add docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/debian {{ ansible_facts['ansible_distribution_release'] }} stable"
state: present
filename: docker-ce
Run Code Online (Sandbox Code Playgroud)
我希望使用变量ansible_facts['ansible_distribution_release']
来获取操作系统发行版名称,在我的例子中,它应该是buster。但它遇到了这样的错误
“该任务包含一个带有未定义变量的选项。错误是:‘dict object’没有属性‘ansible_distribution_release’
我尝试直接使用{{ ansible_distribution_release }}
,有效
repo: "deb [arch=amd64] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable"
Run Code Online (Sandbox Code Playgroud)
然后我想我应该只直接访问事实,而不是作为变量 ansible_facts 的键来访问它,但是后来我阅读了官方文档,我看到了这样的用例
{{ ansible_facts['devices']['xvda']['model'] }}
这让我怀疑我对 ansible 变量的理解有问题
我试图不在, ie,ansible_distribution_release
中引用,但没有运气[]
ansible_facts[ansible_distribution_release]
我运行下面的命令
$ ansible localhost -m setup -a "filter=ansible_distribution_release"
localhost | SUCCESS => {
"ansible_facts": {
"ansible_distribution_release": "buster"
},
"changed": false
}
Run Code Online (Sandbox Code Playgroud)
从而证明确实有一个名为ansible_distribution_release
under的属性ansible_facts
。
任何帮助将不胜感激
udpate:我使用如下所示的说明
- name: debug
block:
- debug:
var: distribution_release
- debug:
var: ansible_distribution_release
- debug:
var: "{{ ansible_facts.keys() }}"
tags: show
Run Code Online (Sandbox Code Playgroud)
并且 finddistribution_release
没有定义, ansible_distribution_release
可以直接访问,但是没有像ansible_distribution_release
ansible_facts中那样的键,但是确实有一个名为 的键distribution_release
。这与输出的差异
ansible localhost -m setup
Run Code Online (Sandbox Code Playgroud)
文件说
INJECT_FACTS_AS_VARS
事实在 ansible_facts 变量中可用,此设置还将它们作为自己的变量推送到主命名空间中。与 ansible_facts 字典内部不同,它们将有一个 ansible_ 前缀。
看来我可以在没有 ansible_ 前缀的情况下访问主空间中的事实
查看主机可用的所有变量(在本例中为 localhost)
- hosts: localhost
tasks:
- debug:
var: hostvars.localhost
Run Code Online (Sandbox Code Playgroud)
下面的调试任务是等效的
- debug:
msg: "{{ ansible_facts['distribution_release'] }}"
- debug:
msg: "{{ ansible_distribution_release }}"
Run Code Online (Sandbox Code Playgroud)
请参阅缓存事实
归档时间: |
|
查看次数: |
8411 次 |
最近记录: |