/ ansible

Assertions in Ansible

You can emulate assertions in Ansible using a combination of registers, the fail module and when directive:

- name: get installed version
  # Get first two version numbers (major.minor)
  shell: node -v | sed -rne "s/^v([0-9]\.[0-9]+).*/\1/p"
  register: node_version

- fail: "Node should be version 0.8"
  when: node_version.stdout != "0.8"

The first task runs a shell command, and stores the resulting stdout and stderr in a register. The next task conditionally fails the playbook if the register does not equal "0.8".

Note that some commands (e.g. python --version) are actaully returned on stderr.