【问题标题】:Removing multiple rpms using ansible on RHEL machines在 RHEL 机器上使用 ansible 删除多个 rpm
【发布时间】:2020-10-24 05:36:23
【问题描述】:

我希望从一组服务器中删除与厨师相关的 rpm。这在剧本中就足够了吗?

第一个选项:

- name: Check if chef rpms exist
  shell: rpm -qa *chef*
 register: rpm_output

- name: Remove chef rpms if they exist
  shell: rpm -e rpm_output
  when: rpm_output.stat.exists

第二个选项:

- name: remove the chef package
  yum:
    name: chef*
    state: absent

如果输出列出了多个 rpm,上述两个 playbook 是否会删除多个 rpm?

提前致谢!

【问题讨论】:

  • ansible yum 模块似乎不接受通配符...

标签: ansible rpm rhel


【解决方案1】:

这是在 ansible 中执行此操作的正确方法,全部使用 yum 模块。

您将不得不使用它两次:一次列出已安装的软件包,另一次删除选定的软件包。

对于后面的这个操作,关键是过滤掉前面操作的结果,只得到需要的名字列表,直接传给yum。

    - name: List installed packages
      yum:
        list: installed
      register: yum_installed

    - name: Remove all packages starting with chef
      yum:
        name: "{{ yum_installed.results | map(attribute='name') | select('search', '^chef.*') | list }}"
        state: absent

或者,您可以使用 json_query 获得相同的结果:

        name: "{{ yum_installed.results | to_json | from_json | json_query(\"[?starts_with(name, 'chef')].name[]\") | list }}"

注意:to_json | from_json 是当前bugjson_query 和 jmespath 库之间的通信中的工作区

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 2015-09-17
    • 2019-05-28
    • 2020-12-28
    相关资源
    最近更新 更多