簡體   English   中英

我如何確認使用 ansible 安裝了 python package

[英]How would I confirm that a python package is installed using ansible

我正在寫一本ansible劇本,我想先安裝pew ,然后過渡到那個 pew 環境,以便安裝其他一些 python 庫。

所以,我的劇本將看起來像這樣......

tasks:

# task 1
- name: install pew if necessary
  command: pip install pew

# task 2
- name: create new pew environment if necessary
  command: pew new devpi-server

# task 3
- name: transition to pew environment
  command: pew workon devpi-server

# task 4
- name: install devpi-server
  command: pip install devpi-server

# task 5
- name: run devpi server
  command: devpi-server ## various args

本着保持我的劇本冪等的精神,我只想在必要時才完成所有這些任務。

所以,如果還沒有安裝 pew,我只想安裝它,如果它還不存在,我只想創建一個新的 pew 環境,如果我們還沒有,我只想在 pew 環境上工作……等等。 ..

有沒有人對如何實現這一目標有好的建議? 我對 ansible 條件句有些熟悉,但只有當它類似於“文件是否已下載”時

我還沒有確定是否安裝了程序,是否加載了虛擬環境等。

您可以使用Ansible中的Pip模塊來確保安裝某些軟件包。

對於你的條件,我會參考: 如果沒有安裝包,如何使Ansible執行shell腳本http://docs.ansible.com/ansible/playbooks_conditionals.html#register-variables - 這些應該讓你在右邊跟蹤。

所以你的劇本看起來有點像:

- pip: name=pew

- name: Check if pew env exists
  command: pew command
  register: pew_env_check

- name: Execute script if pew environment doesn't exist
  command: somescript
  when: pew_env_check.stdout.find('no packages found') != -1

對於 Ansible 任務 - 以下是一些示例:

- name: Install bottle python package
  ansible.builtin.pip:
    name: bottle

- name: Install bottle python package on version 0.11
  ansible.builtin.pip:
    name: bottle==0.11

- name: Install bottle python package with version specifiers
  ansible.builtin.pip:
    name: bottle>0.10,<0.20,!=0.11

- name: Install multi python packages with version specifiers
  ansible.builtin.pip:
    name:
      - django>1.11.0,<1.12.0
      - bottle>0.10,<0.20,!=0.11

參考: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM