Search code examples
ansibleurijirauntil-loop

Ansible "until" response from Jira API


Im trying to schedule an ansible playbook to re-index Jira, Id also like our dev to be able to kick this off ad-hoc is needed which they would be able to do via AWX, I am able to start a re-index but would like a notification when the re-index is complete.

Here are my CLI commands and outputs:

[root@devjira02 ~]# curl -u admin:admin123! -X GET https://devjira.comapny.com/rest/api/2/reindex/progress
{"progressUrl":"/secure/admin/jira/IndexProgress.jspa?taskId=12720","currentProgress":35,"currentSubTask":"Currently working on: Issue index","type":"BACKGROUND","submittedTime":"2019-04-01T14:23:50.971+0100","startTime":"2019-04-01T14:23:50.973+0100","success":false}

[root@devjira02 ~]# curl -u admin:admin123! -X GET https://devjira.company.com/rest/api/2/reindex/progress
{"progressUrl":"/secure/admin/jira/IndexProgress.jspa?taskId=12720","currentProgress":100,"type":"BACKGROUND","submittedTime":"2019-04-01T14:23:50.971+0100","startTime":"2019-04-01T14:23:50.973+0100","finishTime":"2019-04-01T14:24:33.922+0100","success":true}

My Playbook:

- name: DEV Jira Re-index
  become: true
  become_method: sudo
  become_user: root

  hosts: DEVJIRA02
  gather_facts: yes

  tasks:

    - name: Jira Re-index
      uri:
       url: https://devjira.company.com/rest/api/2/reindex?type=BACKGROUND_PREFERRED
       method: POST
       user: admin
       password: admin123!
       force_basic_auth: yes
       status_code: [201, 202]

    - name: Wait until re-index completes
      uri:
        url: https://devjira.company.com/rest/api/2/reindex/progress
        method: GET
        user: admin
        password: admin123!
        force_basic_auth: yes
        status_code: [201, 202]
        return_content: yes
        body_format: json
      register: result
      until: result.json.success == true
      retries: 180
      delay: 30

Id like the second part to retry until success == true at which point AWX can send a notification email. Running the playbook manually will start the re-index but the retry will just retry even though the re-index has finished


Solution

  • Edit after user comment:

    Although my below proposition is still good practice and will prevent some possible future headaches, it proved not to be the issue in this case.

    I didn't pay attention at first that your are requesting the url call to return a 201 or 202 status while the jira documentation specifies you will receive a 200.

    You should either:

    • Change the expected return code: status: 200
    • Drop the status option completely and change your condition: until: response.status == 200 and (result.json.success | bool)

    Original proposition (does not fix but still good practice)

    It is generally a bad idea in ansible to test against a literal true or false because ansible/yaml is quite permissive about the values you can use for that (yes, true, Y, 1, 234, no, false, 0, ...) and also because the values can sometimes be interpreted as pure strings rather that booleans (try with vars_prompt to understant the problem if you have some time).

    Ansible provides a filter to test the boolean representation of a value. I'm not 100% sure this will fix your current problem but I would try with until: result.json.success | bool