AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 644082
Accepted
Adam Matan
Adam Matan
Asked: 2014-11-14 04:56:02 +0800 CST2014-11-14 04:56:02 +0800 CST 2014-11-14 04:56:02 +0800 CST

使用 ansible 运行 apt-get autoremove

  • 772

我用 ansible 维护了一群 EC2 服务器。服务器使用apt 模块定期更新和升级。

当我手动尝试升级服务器时,我收到以下消息:

$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  linux-headers-3.13.0-29 linux-headers-3.13.0-29-generic
  linux-headers-3.13.0-32 linux-headers-3.13.0-32-generic
  linux-image-3.13.0-29-generic linux-image-3.13.0-32-generic
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

有没有办法sudo apt-get autoremove用ansible运行?

apt
  • 6 6 个回答
  • 21331 Views

6 个回答

  • Voted
  1. Best Answer
    oalders
    2016-04-15T05:46:42+08:002016-04-15T05:46:42+08:00

    apt-get自 2.1 版起,对选项的支持--auto-remove现已内置到 Ansible 的apt(选项autoremove)中 官方文档位于http://docs.ansible.com/ansible/apt_module.html

    - name: Remove dependencies that are no longer required
      apt:
        autoremove: yes
    

    合并发生在这里。

    请注意,autoclean从 2.4 开始也可以使用

    • 31
  2. cortopy
    2015-06-16T10:53:42+08:002015-06-16T10:53:42+08:00

    这种简化的方法只需要一项任务

      - name: Autoremove unused packages
        command: apt-get -y autoremove
        register: autoremove_output
        changed_when: "'The following packages will be REMOVED' in autoremove_output.stdout"
    
    • 14
  3. Antonis Christofides
    2014-11-14T05:55:05+08:002014-11-14T05:55:05+08:00

    您可以使用command(未​​经测试):

      - name: Check if anything needs autoremoving
        shell: apt-get -y --dry-run autoremove | grep -q "0 to remove"
        register: check_autoremove
        ignore_errors: True
        changed_when: False
        always_run: True
    
      - name: Autoremove unused packages
        command: apt-get -y autoremove
        when: "check_autoremove.rc != 0"
    

    但是,我认为自动运行可能会有风险autoremove。由于您过去犯过的系统管理错误(这些错误可能在您的 ansible 代码中),因此可能会在某些时候将所需的包错误地检测为可自动删除,这可能会导致服务器停止工作。另一方面,将未使用的软件包留在系统上也没什么大不了的,除非您对服务器的设置进行重大更改,否则这种情况并不常见。

    因此,我不会在没有人确认的情况下自动删除包裹。

    • 10
  4. Marwan Alsabbagh
    2015-04-15T00:22:56+08:002015-04-15T00:22:56+08:00

    这是 Antonis Christofides 提供的解决方案的一个变体。它已经过测试并且对我有用。我避免在 check 命令中使用 ignore_errors。否则,它通常采用相同的方法。

    - name: Check if packages need to be autoremoved
      command: apt-get --dry-run autoremove
      register: check_autoremove
      changed_when: False
    - name: Autoremove unused packages
      command: apt-get -y autoremove
      when: "'packages will be REMOVED' in check_autoremove.stdout"
    
    • 6
  5. Martin Tapp
    2015-06-18T10:25:40+08:002015-06-18T10:25:40+08:00

    突出显示包变化的变体(第一个任务将适当地涂成绿色或黄色):

      - name: check if packages need to be autoremoved
        shell: apt-get --dry-run autoremove | grep "to remove" | sed "s/^[0-9]\+ upgraded, [0-9]\+ newly installed, \([0-9]\+\) to remove and [0-9]\+ not upgraded\.$/\1/"
        register: check_autoremove
        changed_when: check_autoremove.stdout != "0"
    
      - name: autoremove unused packages
        command: apt-get -y autoremove
        when: check_autoremove.changed
    
    • 2
  6. Chu-Saing Lai
    2016-10-11T21:11:46+08:002016-10-11T21:11:46+08:00

    我喜欢这种简化的方法,并为我添加了一些检查和打印信息。

    #!/usr/bin/env ansible-playbook
    ---
    
    - name: Autoremove 'apt' package for Debian, Ubuntu
      hosts: all
    
      pre_tasks:
        - name: check storage space - before
          shell: df -h
          register: check_storage_space_before
    
        - name: print storage space
          debug:
            msg: "{{ check_storage_space_before.stdout_lines }}"
    
        - name: apt autoremove check 
          command: apt-get -y --dry-run autoremove
          register: apt_autoremove_output
    
        - name: print apt autoremove packages
          debug:
            msg: "{{ apt_autoremove_output.stdout_lines }}"
    
      tasks:    
        - name: autoremove unused packages
          become: yes
          command: apt-get -y autoremove
          changed_when: "'The following packages will be REMOVED' in apt_autoremove_output.stdout"
    
      post_tasks:
        - name: check storage space - after
          shell: df -h
          register: check_storage_space_after
    
        - name: print storage space
          debug:
            msg: "{{ check_storage_space_after.stdout_lines }}"
    
    # vim: ft=ansible :
    

    感谢cortopy和Dave James Miller。

    • 1

相关问题

  • Debian 的免费集中补丁管理

  • 元管理 Debian 软件包配置文件的最佳方法是什么?

  • 我如何告诉 apt 永远不要升级特定的软件包?

  • 我应该多久更新一次我们的 Linux 服务器?

  • 能力/易于获得。有什么方法可以在搜索中只显示非 X 依赖的应用程序?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve