Roman Dryndik
Roman Dryndik

Reputation: 1184

Some tasks in ansible are't executed

I'm using ansible for some deployment issues.

I want to do the following:

  1. Install virtualenv
  2. Activate installed virtual environment
  3. Check if I'm in virtual environment

For this purposes I have the following playbook:

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

The problem is that sometimes some tasks are not executed, but they have to... For instance in my case the task:

  tasks:
    - name: create virtualenv
      command: virtualenv venv

Is not executed.

But if I will comment 2 last tasks:

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

The previous one works...

Can't get what I'm doing wrong. Can somebody hint me?

Upvotes: 0

Views: 157

Answers (1)

300D7309EF17
300D7309EF17

Reputation: 24603

assuming hosts: servers covers the correct servers, you should only have one tasks entry. Here's an optimized and simplified playbook.

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

Note I've cached the apt call and I've also changed state to present. You likely want to install specific versions, rather than rechecking on every run of ansible.

Upvotes: 2

Related Questions