通过 Ansible 更新操作系统确定主要版本

通过 Ansible 更新操作系统确定主要版本

我创建了一个 Ansible 角色,它仅包含任务和一些变量。目前可能有点过度,但我计划在将来的某个时候扩展该角色。我首先尝试确定正在运行的操作系统(在我的情况下是 RHEL 7 或 8),然后使用 yum 或 dnf 进行更新。

这是我用来包含该角色的剧本:

$ more playbooks/UpdateAllPackages.yml

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

角色:

$ more role/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 Yum Update Encounters Any Errors
  debug:
    msg: "Dnf Update Encounted Errors Please Investigate"
  when: DnfStandardOutput is not defined

以及相关变量:

$ more 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-solut
ions-e4s-rpms,rhel-?-for-x86_64-sap-solutions-eus-rpms,rhel-?-for-x86_64-sap-solutions-rpms"

当我运行剧本时

$ 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
}

然后我想也许在剧本中gather_facts设置为 会存在一些问题,所以我将其更改为。但同样的问题/错误仍然存​​在。noyes

谢谢你!

答案1

根据错误信息

'dict object' has no attribute 'ansible_distribution_major_version'

似乎没有收集到事实。为了调试目的并缩小环境中的原因,一个最小示例剧本

---
- hosts: localhost
  become: false
  gather_facts: true
  gather_subset:
    - distribution
    - '!all'
    - '!min'

  tasks:

  - debug:
      var: ansible_facts

  - debug:
      msg: "{{ ansible_distribution_major_version }}"

将导致输出

TASK [debug] **********************************
ok: [localhost] =>
  ansible_facts:
    distribution: RedHat
    distribution_file_parsed: true
    distribution_file_path: /etc/redhat-release
    distribution_file_search_string: Red Hat
    distribution_file_variety: RedHat
    distribution_major_version: '7'
    distribution_release: Maipo
    distribution_version: '7.9'
    gather_subset:
    - distribution
    - '!all'
    - '!min'
    module_setup: true
    os_family: RedHat

TASK [debug] **********************************
ok: [localhost] =>
  msg: '7'

进一步阅读

相关内容