大网数据广告活动
服务器运维问答

ansible: why I cannot use {{ ansible_facts['ansible_distribution_release'] }} in playbook

· 52CE 我爱测

I have ansible task run on localhost like this - name: add docker repository apt_repository: repo: "deb [arch=amd64] https://download.docker.com/linux/debian {{ ansible_facts['ansible_distribution_release'] }} stable" state: pres…

问题原文

原帖链接 · Roy · CC BY-SA 4.0

I have ansible task run on localhost like this

  - 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

I wish to use variable ansible_facts['ansible_distribution_release'] to get the os distribution name, in my case, it should be buster. But it runs into an error like this

"The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'ansible_distribution_release'

I tried to use {{ ansible_distribution_release }} directly, and it works

repo: "deb [arch=amd64] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable"

Then I thought I should only access the facts directly, not access it as a key of the variable ansible_facts, but then I read the official document, I see use cases like

{{ ansible_facts['devices']['xvda']['model'] }}

It make me suspicious there is something wrong about my understanding of ansible variables

I've tried to not quote ansible_distribution_release in the [], ie, ansible_facts[ansible_distribution_release], but without luck

I run command below

$ ansible localhost -m setup -a "filter=ansible_distribution_release"

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_release": "buster"
    },
    "changed": false
}

thus proved there did have an attribute named ansible_distribution_release under ansible_facts.

Any help will be appreciated


udpate: I use instructions show below

- name: debug                                                                                                                                                                                                      
  block:                                                                                                                                                                                                           
    - debug:                                                                                                                                                                                                       
        var: distribution_release                                                                                                                                                                        

    - debug:                                                                                                                                                                                                       
        var: ansible_distribution_release                                                                                                                                                                          

    - debug:                                                                                                                                                                                                       
        var: "{{ ansible_facts.keys() }}"                                                                                                                                                                          
  tags: show       

and find out distribution_release is not defined, ansible_distribution_release can be directly accessed, but there is no such key as ansible_distribution_release in ansible_facts, but there do have a key named distribution_release. this is difference from the out put from

ansible localhost -m setup

the documents says

INJECT_FACTS_AS_VARS

Facts are available inside the ansible_facts variable, this setting also pushes them as their own vars in the main namespace. Unlike inside the ansible_facts dictionary, these will have an ansible_ prefix.

It seems I can access facts in the mainspace without the ansible_ prefix

已采纳解答

原帖链接 · Vladimir Botka · CC BY-SA 4.0

Take a look at all variables available to the host (localhost in this case)

- hosts: localhost
  tasks:
    - debug:
        var: hostvars.localhost

The debug tasks below are equivalent

    - debug:
        msg: "{{ ansible_facts['distribution_release'] }}"
    - debug:
        msg: "{{ ansible_distribution_release }}"

See Caching facts

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-06-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。