使用 ansible 脚本执行 yum clean expire-cache 和 remove 命令
Executing yum clean expire-cache and remove commands using ansible script
任何人都可以支持清理和删除的等效任务吗?
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;DR;
以下是您的等效任务:
- 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
。
安装时会测试installed
或present
状态,删除时测试removed
或absent
状态。
安装:
- name: install
yum:
name: pkg-to-install
state: present
注意: yum install
和 state: latest
是不一样的,当 yum
命令安装时如果包不存在并且如果它已经存在则什么都不做,如果包不存在,state: latest
将进行安装,但如果包不是最新版本, 也会 和 yum update pkg-to-install
。
真正的等价物是 state: present
.
present
and installed
will simply ensure that a desired package is installed.
latest
will update the specified package if it's not of the latest available version.
来源:https://docs.ansible.com/ansible/latest/modules/yum_module.html#parameter-state
删除:
- name: remove
yum:
name: pkg-to-remove
state: absent
然后对于 clean
,可悲的是,可以选择不实施它,因为这不是可以以幂等方式完成的事情。
在 yum
模块页面上查看此注释
- The yum module does not support clearing yum cache in an idempotent way, so it was decided not to implement it, the only method is to use command and call the yum command directly, namely “command: 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
所以它们是等价的:
- 在bash
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 expire-cache
yum -y remove packageX-S
yum -y install packageX-S
我已经安装好了...
- name: deploy
yum:
name: llc-html-clients-S
state: latest
TL;DR;
以下是您的等效任务:
- 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
。
安装时会测试installed
或present
状态,删除时测试removed
或absent
状态。
安装:
- name: install
yum:
name: pkg-to-install
state: present
注意: yum install
和 state: latest
是不一样的,当 yum
命令安装时如果包不存在并且如果它已经存在则什么都不做,如果包不存在,state: latest
将进行安装,但如果包不是最新版本, 也会 和 yum update pkg-to-install
。
真正的等价物是 state: present
.
present
andinstalled
will simply ensure that a desired package is installed.
latest
will update the specified package if it's not of the latest available version.
来源:https://docs.ansible.com/ansible/latest/modules/yum_module.html#parameter-state
删除:
- name: remove
yum:
name: pkg-to-remove
state: absent
然后对于 clean
,可悲的是,可以选择不实施它,因为这不是可以以幂等方式完成的事情。
在 yum
模块页面上查看此注释
- The yum module does not support clearing yum cache in an idempotent way, so it was decided not to implement it, the only method is to use command and call the yum command directly, namely “command: 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
所以它们是等价的:
- 在bash
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