-2

Below is my Ansible task I want to make sure if its correct or not

- name: check
    shell: curl -s 'http://<host>/path' | grep abc
       warn=no
    no_log: True
    register: grep_output
    ignore_errors: true
    failed_when: grep_output.stdout != ''
U880D
  • 8,601
  • 6
  • 24
  • 40
KGC
  • 41
  • 4

1 Answers1

0

I understand your question that you are interested in verifying grep results (first). As already mentioned in the comments, you will need to define what failure means.

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    ANSIBLE_TOWER_URL: "yourURL"
    TOWER_LATEST_VERSION: "yourVersion"

  tasks:

  - name: Check Ansible Tower version
    shell:
      cmd: curl --silent --location "https://{{ ANSIBLE_TOWER_URL }}/api/v2/ping/" | jq '.version' | grep {{ TOWER_LATEST_VERSION }}
      warn: false
    register: result
    failed_when: result.rc != 0
    changed_when: false
    check_mode: false

  - name: Show result
    debug:
      msg: "{{ result.stdout }}"

You should not use the ignore_errors directive since

It does not make Ansible ignore undefined variable errors

later. Furthermore the above approach does not take HTTP Status Codes in count as the following example shows.

curl --silent --location "https://{{ ANSIBLE_TOWER_URL }}/api/v2/ping/" --write-out "%{http_code}" | jq '.'

So depending on the use case it would not be clear if the expected result was there or not, or if the page was not reachable or accessible and false result are given and incorrect assumptions are made.

Therefore it might be better to use the uri module to interact with webservices like

  - name: Check Ansible Tower version
    uri:
      url: "https://{{ ANSIBLE_TOWER_URL }}/api/v2/ping/"
      method: GET
      validate_certs: yes
      return_content: yes
      status_code: 200
      body_format: json
    check_mode: false
    register: result

  - name: Show result
    debug:
      msg: "{{ result.json }}"

with which you could access values of the result set like "{{ result.json.version }}".

Further Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40