Ansible - Task Status

> Infrastructure as code > Ansible

1 - About

Article about the task status

Advertising

3 - Example

3.1 - Modify the status condition

With changed_when and failed_when

- name: Testing the JDBC connectivity
  command: "{{ sqlline_home }}/sqlline -u 'jdbc:sqlserver://{{ infa_db_endpoint }}:{{ infa_db_port }};databaseName={{ infa_domain_db_name }}' -n '{{ infa_db_user }}' -p '{{ infa_db_password }}' -e 'select 1'"
  register: jdbc_test_result
  changed_when: "jdbc_test_result.rc != 0"
  failed_when: "jdbc_test_result.rc != 0"

3.2 - Ignore error

The error will not be counted as a failure.

  - name: 'Check network connectivity to ldap ({{ ldap_host }})'
    wait_for:
      host: '{{ ldap_host }}'
      port: "{{ item }}"
      state: started         # Port should be open
      delay: 0               # No wait before first check (sec)
      timeout: 3             # Stop checking after timeout (sec)
    ignore_errors: yes
    with_items:
    - '{{ ldap_port }}'

3.3 - Conditional Task

Doc

tasks:

  - shell: /usr/bin/foo
    register: result
    ignore_errors: True

  - debug:
      msg: "it failed"
    when: result is failed
 
  # in most cases you'll want a handler, but if you want to do something right now, this is nice
  - debug:
      msg: "it changed"
    when: result is changed

  - debug:
      msg: "it succeeded in Ansible >= 2.1"
    when: result is succeeded

  - debug:
      msg: "it succeeded"
    when: result is success

  - debug:
      msg: "it was skipped"
    when: result is skipped

4 - Documentation / Reference

infra/ansible/status.txt · Last modified: 2019/09/10 15:56 by gerardnico