我希望从一组服务器中删除与厨师相关的rpms。这在一本剧本里就够了吗?
第一种选择:
- 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吗?
提前感谢!
发布于 2020-07-03 14:21:24
在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是当前错误在json_query与jmespath库之间的通信中的一个解决方案。
https://stackoverflow.com/questions/62716348
复制相似问题