ansible中的一些任务不会被执行

时间:2014-06-26 11:42:55

标签: deployment web-deployment ansible ansible-playbook

我正在使用ansible来解决部署问题。

我想做以下事情:

  1. 安装virtualenv
  2. 激活已安装的虚拟环境
  3. 检查我是否在虚拟环境中
  4. 为此,我有以下剧本:

    ---
    - 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
    

    未执行。

    但如果我要评论最后两项任务:

      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 == ""
    

    前一个有效...

    无法得到我做错的事。有人能暗示我吗?

1 个答案:

答案 0 :(得分: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来电,我还将state更改为present。您可能希望安装特定版本,而不是每次运行ansible时重新检查。