0

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

我想做以下事情:

  1. 安装虚拟环境
  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

不被执行。

但是,如果我要评论最后两个任务:

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

上一个有效...

无法理解我做错了什么。有人可以提示我吗?

4

1 回答 1

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.

于 2014-06-26T17:23:57.550 回答