Skip to main content
 首页 » 编程设计

python之使用ansible检查python模块版本

2024年10月01日9emanlee

有什么方法可以创建 ansible playbook 来找出目标主机上当前安装的 python 模块版本?我不想安装指定版本,我只想知道它是否存在(示例响应已安装模块:版本 12.03未安装模块)

我发现使用 pip ansible 模块,没有状态可以确保是否安装了某些东西,唯一的可能性是安装或删除

请您参考如下方法:

这对于 POSIX 系统怎么样?

- name: check version of pytz 
  shell: pip show pytz | grep Version | cut -d ' ' -f 2 
  ignore_errors: true 
  changed_when: false 
  register: pytz_version 

只打印模块版本:

- debug: var=pytz_version.stdout 

您还可以在其他任务中将版本用作“when”子句:

- name: Do something requiring pytz 2012d 
  command: foo 
  when: pytz_version.stdout | search("2012d")