You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible更新RHEL系统时无法获取ansible_distribution_major_version属性的问题求助

Ansible更新RHEL系统时无法获取ansible_distribution_major_version属性的问题求助

我最近写了一个Ansible角色,用来给RHEL7或8系统更新包,目前只有几个任务和变量,虽然现在看起来有点小题大做,但我打算之后扩展它。核心逻辑是先判断系统版本,再用yum(RHEL7)或dnf(RHEL8)执行更新。

我的Playbook(playbooks/UpdateAllPackages.yml):

---
- name: Update All Packages
  hosts: all
  gather_facts: no
  roles:
    - { role: roles/UpdateAllPackages }

角色的任务文件(roles/UpdateAllPackages/tasks/main.yml):

---
- name: Update All RHEL 7 Packages Via YUM
  ansible.builtin.yum:
    name: "{{ PkgInclude }}"
    state: "{{ PkgVers  }}"
    enablerepo: "{{ RepoEnable }}"
    disablerepo: "{{ RepoDisable }}"
  when: ansible_facts['ansible_distribution_major_version']=="7"
  register: YumStandardOutput

- name: Print Errors If The Yum Update Encounters Any Errors
  ansible.builtin.debug:
    msg: "Yum Update Encounted Errors Please Investigate"
  when: YumStandardOutput is not defined

- name: Update All RHEL 8 Packages Via DNF
  ansible.builtin.dnf:
    name: "{{ PkgInclude }}"
    state: "{{ PkgVers  }}"
    enablerepo: "{{ RepoEnable }}"
    disablerepo: "{{ RepoDisable }}"
  when: ansible_facts['ansible_distribution_major_version']=="8"
  register: DnfStandardOutput

- name: Print Errors If The Dnf Update Encounters Any Errors
  debug:
    msg: "Dnf Update Encounted Errors Please Investigate"
  when: DnfStandardOutput is not defined

角色的默认变量(roles/UpdateAllPackages/defaults/main.yml):

---
PkgInclude: "*"
PkgExclude: ""
PkgVers: "latest"
RepoEnable: "rhel-?-for-x86_64-appstream-e4s-rpms,rhel-?-for-x86_64-appstream-eus-rpms,rhel-?-for-x86_64-appstream-rpms,rhel-?-for-x86_64-baseos-e4s-rpms,rhel-?-for-x86_64-baseos-eus-rpms,rhel-?-for-x86_64-baseos-rpms,ansible-2-for-rhel-?-x86_64-rpms"
RepoDisable: "rhel-?-for-x86_64-sap-netweaver-e4s-rpms,rhel-?-for-x86_64-sap-netweaver-eus-rpms,rhel-?-for-x86_64-sap-netweaver-rpms,rhel-?-for-x86_64-sap-solutions-e4s-rpms,rhel-?-for-x86_64-sap-solutions-eus-rpms,rhel-?-for-x86_64-sap-solutions-rpms"

执行命令及报错信息:

我用这条命令执行Playbook:

ansible-playbook -i inventories/<name of inventory>.ini --limit=<name of server> playbooks/UpdateAllPackages.yml --extra-vars=@./vault/<name of vault>.yml

结果报错:

PLAY [Update All Packages] *************************************************************************************************************************************

TASK [UpdateAllPackages : Update All RHEL 7 Packages Via YUM] **************************************************************************************************
fatal: [<name of server>]: FAILED! => {"msg": "The conditional check 'ansible_facts['ansible_distribution_major_version']==\"7\"' failed. The error was: error while evaluating conditional (ansible_facts['ansible_distribution_major_version']==\"7\"): 'dict object' has no attribute 'ansible_distribution_major_version'\n\nThe error appears to be in '/home/ansible/roles/UpdateAllPackages/tasks/main.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Update All RHEL 7 Packages Via YUM\n  ^ here\n"}

我做的排查:

我理解这个错误是说ansible_distribution_major_version这个属性不存在。于是我在控制节点上执行了这条命令测试:

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

得到的结果是正常的:

[WARNING]: No inventory was parsed, only implicit localhost is available
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_major_version": "7"
    },
    "changed": false
}

我一开始以为是Playbook里gather_facts: no的问题,于是改成了gather_facts: yes,但还是出现同样的错误。

有没有大佬能帮我看看这是怎么回事?

备注:内容来源于stack exchange,提问作者user2090974

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 08:03:12