Skip to content

Instantly share code, notes, and snippets.

@acardak
Forked from ParagDoke/main.yml
Created December 19, 2018 03:23
Show Gist options
  • Save acardak/7d67762c98e2d40e3ac6f795a6699638 to your computer and use it in GitHub Desktop.
Save acardak/7d67762c98e2d40e3ac6f795a6699638 to your computer and use it in GitHub Desktop.

Revisions

  1. @ParagDoke ParagDoke revised this gist Sep 12, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion status-poller.yml
    Original file line number Diff line number Diff line change
    @@ -25,5 +25,5 @@
    msg: "{{ debug_msg_prefix | default('') }}{{ response.json | json_query(json_query_to_build_debug_msg) }}{{ debug_msg_suffix | default('') }}"
    when: display_debug_msg_for_each_attempt | bool and json_query_to_build_debug_msg is defined

    - include: includes/status-poller.yml
    - include_tasks: includes/status-poller.yml
    when: poll_condition
  2. @ParagDoke ParagDoke created this gist Sep 12, 2018.
    18 changes: 18 additions & 0 deletions main.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    ---
    - name: test
    hosts: localhost
    connection: local
    tasks:
    - name: Invoke poller
    vars:
    url: http://localhost:8000/abc.json
    validate_certs: yes
    poll_interval: 10
    poll_condition: '"failed" not in response.content and response.json.status=="running"'
    fail_condition: '"failed" in response.content'
    max_poll_attempts: 5
    display_debug_msg_for_each_attempt: yes
    json_query_to_build_debug_msg: "tasks[?status=='running'] | [0].name"
    debug_msg_prefix: 'Prefix:'
    debug_msg_suffix: ''
    include_tasks: status-poller.yml
    29 changes: 29 additions & 0 deletions status-poller.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    ---
    - set_fact:
    attempts: "{{ attempts | default(0) | int + 1 }}"

    - fail: msg="Number of attempts exceeded {{ max_poll_attempts }}"
    when: (attempts | int) > (max_poll_attempts | int)

    - debug:
    msg: "Attempt: {{ attempts }}/{{ max_poll_attempts }}"

    - pause:
    seconds: "{{ poll_interval }}"
    no_log: true

    - name: Fetch status afresh
    no_log: true
    uri:
    url: "{{ url }}"
    return_content: yes
    validate_certs: "{{ validate_certs }}"
    register: response
    failed_when: fail_condition

    - debug:
    msg: "{{ debug_msg_prefix | default('') }}{{ response.json | json_query(json_query_to_build_debug_msg) }}{{ debug_msg_suffix | default('') }}"
    when: display_debug_msg_for_each_attempt | bool and json_query_to_build_debug_msg is defined

    - include: includes/status-poller.yml
    when: poll_condition