执行 yum 清理过期缓存并使用 ansible 脚本删除命令



任何人都可以支持清理和删除的等效任务吗?

yum clean expire-cache
yum -y remove packageX-S
yum -y install packageX-S

我已经安装了...

- name: deploy
yum:
name: llc-html-clients-S
state: latest

TL;博士;

以下是您的等效任务:

- name: clean
command: yum clean expire-cache
- name: remove
yum:
name: pkg-to-remove
state: absent
- name: install
yum:
name: pkg-to-install
state: present

安装和拆卸使用相同的模块yum完成。

当安装将测试installedpresent状态时,删除是关于测试removedabsent状态。

安装:

- name: install
yum:
name: pkg-to-install
state: present

注意:yum installstate: latest是不一样的,如果软件包不存在,yum命令将安装,如果它已经存在,则不执行任何操作,如果软件包不存在,state: latest将执行安装,但如果软件包不是最新版本,则也会执行yum update pkg-to-install
真正的等价物是state: present.

presentinstalled将简单地确保安装了所需的软件包。
如果指定的包不是最新的可用版本,latest将更新它。

来源:https://docs.ansible.com/ansible/latest/modules/yum_module.html#parameter-state

删除:

- name: remove
yum:
name: pkg-to-remove
state: absent

然后对于clean,可悲的是,可以选择不实现它,因为这不是可以以幂等方式完成的事情。

请参阅模块页面上yum此说明

  • yum 模块不支持以幂等方式清除 yum 缓存,因此决定不实现它,唯一的方法是使用 command 并直接调用 yum 命令,即"命令:yum clean all">
    https://github.com/ansible/ansible/pull/31450#issuecomment-352889579

来源: https://docs.ansible.com/ansible/latest/modules/yum_module.html#notes

因此,正如注释中所指出的,您实际上可以通过一个简单的command

- name: clean
command: yum clean expire-cache

所以这些是等效的:

  • 在巴什
yum clean expire-cache
yum -y remove pkg-to-remove
yum -y install pkg-to-install
  • 在剧本中
- name: clean
command: yum clean expire-cache
- name: remove
yum:
name: pkg-to-remove
state: absent
- name: install
yum:
name: pkg-to-install
state: present

最好删除路径而不是使用 yum clean all, 随意使用以下任务进行活动:

---
# tasks file for yum cache clean
- name: Cleaning up yum cache
file:
path: /var/cache/yum/
state: absent
...

如果需要行动手册:使用角色上部作为任务

猫 clear_yum_cache.yml

---
- name: Playbook for clearing yum cache
hosts: all
user: <<adduserhere>>
become: yes
become_method: sudo
become_user: root
gather_facts: True
pre_tasks:
- debug: msg="Beginning clean yum"
roles:
- patch-yum-clean
post_tasks:
- debug: msg="Yum cache clean completed successfully"
...

享受

最新更新