2014-06-26 49 views
0

我使用ansible來解決某些部署問題。某些任務不能執行

我要做到以下幾點:

  1. 安裝的virtualenv
  2. 激活安裝的虛擬環境
  3. 檢查,如果我在虛擬環境中

就是爲了這個目的,我有以下劇本:

--- 
- hosts: servers 

    tasks: 
    - name: update repository 
     apt: update_cache=yes 
     sudo: true 

    tasks: 
    - name: install git 
     apt: name=git state=latest 
     sudo: true 

    tasks: 
    - name: install pip 
     apt: name=python-pip state=latest 
     sudo: true 

    tasks: 
    - name: installing postgres 
     sudo: true 
     apt: name=postgresql state=latest 

    tasks: 
    - name: installing libpd-dev 
     sudo: true 
     apt: name=libpq-dev state=latest 

    tasks: 
    - name: installing psycopg 
     sudo: true 
     apt: name=python-psycopg2 state=latest 

    tasks: 
    - name: configuration of virtual env 
     sudo: true 
     pip: name=virtualenvwrapper state=latest 

    tasks: 
    - name: create virtualenv 
     command: virtualenv venv 

    tasks: 
    - name: virtualenv activate 
     shell: . ~/venv/bin/activate 

    tasks: 
    - name: "Guard code, so we are more certain we are in a virtualenv" 
     shell: echo $VIRTUAL_ENV 
     register: command_result 
     failed_when: command_result.stdout == "" 

的問題是,有時有些任務沒有被執行,但他們必須...例如在我的情況的任務:

tasks: 
    - name: create virtualenv 
     command: virtualenv venv 

不執行。

但是,如果我會評論最後2項任務:

tasks: 
    - name: virtualenv activate 
     shell: . ~/venv/bin/activate 

    tasks: 
    - name: "Guard code, so we are more certain we are in a virtualenv" 
     shell: echo $VIRTUAL_ENV 
     register: command_result 
     failed_when: command_result.stdout == "" 

以前的作品之一......

不能讓我在做什麼錯。有人可以暗示我嗎?

+3

您正在重複「任務:」鍵 - 但您只需要一次。 –

+0

@Ramon de la Fuente謝謝) –

回答

2

假設hosts: servers涵蓋了正確的服務器,您應該只有一個tasks條目。這是一個優化和簡化的手冊。

--- 
- hosts: servers 
    sudo: yes 
    tasks: 
    - name: update repository daily 
    apt: update_cache=yes cache_valid_time=86400 
    - name: install development dependencies 
    apt: name={{item}} state=latest 
    with_items: 
     - git 
     - python-pip 
     - postgresql 
     - libpq-dev 
     - python-psycopg2 
    - name: configuration of virtual env 
    pip: name=virtualenvwrapper state=present 
    - name: create virtualenv 
    command: virtualenv venv 
    - name: virtualenv activate 
    shell: . ~/venv/bin/activate 
    - name: "Guard code, so we are more certain we are in a virtualenv" 
    shell: echo $VIRTUAL_ENV 
    register: command_result 
    failed_when: command_result.stdout == "" 

注意我緩存apt電話,我也改變了statepresent。您可能想安裝特定版本,而不是在每次運行ansible時重新檢查。

相關問題