使用ansible检查python模块版本

rzy*_*sia 5 python pip ansible

有没有办法创建 ansible playbook 来找出目标主机上当前安装的 python 模块版本?我不想安装指定的版本,我只想知道它是否存在(示例响应module installed: version 12.03module not installed

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

Joh*_*n L 3

对于 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
Run Code Online (Sandbox Code Playgroud)

仅打印模块版本:

- debug: var=pytz_version.stdout
Run Code Online (Sandbox Code Playgroud)

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

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