---
- name: Make sure we have an updated copy of the repository
  become_user: "{{ target_user }}"
  git:
    repo: "{{ repo_url }}"
    dest: "{{ target_dir }}"
    update: yes
    version: "{{ repo_branch }}"
    accept_hostkey: True
  register: repo_state

- name: Perform build
  become_user: "{{ target_user }}"
  command: "{{ item }}"
  args:
    chdir: "{{ target_dir }}"
  with_items: target_commands
  when: "repo_state|changed"
  register: command_result
  ignore_errors: true

# if the command failed, roll the git repository back one commit so that
# on the next run, we will re-run the build
- name: Revert git repo
  become_user: "{{ target_user }}"
  command: "git reset --hard HEAD^"
  args:
    chdir: "{{ target_dir }}"
  when: "repo_state|changed and command_result.rc != 0"

- name: Indicate failure
  fail: msg="the buildout command failed"
  when: "repo_state|changed and command_result.rc != 0"

# vim:ts=2:sw=2:noai:nosi

---
- name: Make sure we have an updated copy of the repository
  become_user: "{{ target_user }}"
  git:
    repo: "{{ repo_url }}"
    dest: "{{ target_dir }}"
    update: yes
    version: "{{ repo_branch }}"
    accept_hostkey: True
  register: repo_state

- name: Perform build
  become_user: "{{ target_user }}"
  command: "{{ item }}"
  args:
    chdir: "{{ target_dir }}"
  with_items: target_commands
  when: "repo_state|changed"
  register: command_result
  ignore_errors: true

# if the command failed, roll the git repository back one commit so that
# on the next run, we will re-run the build
- name: Revert git repo
  become_user: "{{ target_user }}"
  command: "git reset --hard HEAD^"
  args:
    chdir: "{{ target_dir }}"
  when: "repo_state|changed and command_result|failed"

- name: Indicate failure
  fail: msg="the buildout command failed"
  when: "repo_state|changed and command_result|failed"

# vim:ts=2:sw=2:noai:nosi

Use the `|failed` filter instead of comparing rc
Use the `|failed` filter instead of comparing rc
