Ansible 禁用回购未按预期运行

Ansible disable repo not functioning as expected

我已经用两个不同的模块试过了,但是我总是得到返回状态:"OK",而不是预期的 "Changed"。检查服务器还显示没有进行任何更改并且 repo 仍然处于活动状态:

- hosts: rh_estate
  user: whatuser
  gather_facts: true
  become: true
  tasks:
    - name: Disable YUM Repo
      yum_repository:
        name: rhui-rhel-7-server-rhui-extras-debug-rpms
        state: absent
      when: ansible_facts['distribution'] == "RedHat"

以及 Yum 模块:

- name: Disable YUM Repo
  yum:
    disablerepo: rhui-rhel-7-server-rhui-extras-debug-rpms
  when: ansible_facts['distribution'] == "RedHat"

我宁愿使用模块而不是文件中的行。我想如果实际上没有其他办法,我更喜欢 shell yum-config-manager --disable rhui-rhel-7-server-rhui-extras-debug-rpms

回购声明:

/etc/yum.repos.d/rh-cloud.repo
[rhui-rhel-7-server-rhui-extras-debug-rpms]
name=Red Hat Enterprise Linux 7 Server - Extras from RHUI (Debug RPMs)
baseurl=https://rhui-1.microsoft.com/pulp/repos//content/dist/rhel/rhui/server/7/7Server/$basearch/extras/debug
        https://rhui-2.microsoft.com/pulp/repos//content/dist/rhel/rhui/server/7/7Server/$basearch/extras/debug
        https://rhui-3.microsoft.com/pulp/repos//content/dist/rhel/rhui/server/7/7Server/$basearch/extras/debug
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
sslverify=1
sslclientcert=/etc/pki/rhui/product/content.crt
sslclientkey=/etc/pki/rhui/key.pem

yum repolist all 的输出:

[root@server ~]# yum repolist all | grep 'repo id\|rhui-rhel-7-server-rhui-extras'
repo id                                                           status
rhui-rhel-7-server-rhui-extras-debug-rpms/x86_64                  enabled:    262
rhui-rhel-7-server-rhui-extras-rpms/x86_64                        enabled:  1,105
rhui-rhel-7-server-rhui-extras-source-rpms/x86_64                 enabled:    430

感谢您提供额外信息,以便我可以正确测试。查看 yum_repository module docfile 参数,其中说明:

File name without the .repo extension to save the repo in. Defaults to the value of name.

在你的情况下,保存你的存储库的文件名不同于你的存储库的 uid(也就是 ansible 上下文中的名称)。您需要根据要求提供文件参数才能完成您的任务:

    - name: Disable YUM Repo
      yum_repository:
        name: rhui-rhel-7-server-rhui-extras-debug-rpms
        file: rh-cloud
        state: absent
      when: ansible_facts['distribution'] == "RedHat"

针对 centos:7 docker 容器快速测试(无条件)。

文档页面底部还有其他几个有用的示例。

注意:使用 disable_repoyum module 只会暂时禁用单个 yum 操作的特定存储库,不会将其从配置中完全删除。来自文档:

These repos will not persist beyond the transaction.