---

- name: "Initilize VARs"
  set_fact:
    awx_type_id: "None"
  changed_when: False

- name: "Search {{ awx_rest_api_type }} informations for {{ awx_search_name }}"
  delegate_to: localhost
  uri:
    url: "{{ awx_base_url }}/api/v2/{{ awx_rest_api_type }}/?search={{ awx_search_name | urlencode }}"
    method: GET
    user: "{{ awx_rest_api_access_user }}"
    password: "{{ awx_rest_api_access_pw }}"
    return_content: true
    validate_certs: false
    force_basic_auth: yes
    status_code: 200
  register: awx_type_info

- name: Save result as variable (fact)
  set_fact: 
    awx_type_info_json: "{{ awx_type_info.json }}"

- name: "Printing {{ awx_rest_api_type }} informations for {{ awx_search_name }}"
  debug:
    msg: "{{ awx_type_info_json }}"
  delegate_to: 127.0.0.1
  become: false
  when:
   - debug

- name: "Search {{ awx_rest_api_type }} id for {{ awx_search_name }}"
  vars:
    query: '[? {{ awx_search_key }}==`{{ awx_search_name }}`].id'
  set_fact:
    awx_type_id: "{{ awx_type_info_json.results | json_query(query) | first | default(None) }}"
  when: (awx_type_info_json.results is defined) and (awx_type_info_json.results | length > 0)

- name: "Printing {{ awx_rest_api_type }}['{{ awx_search_key }}'='{{ awx_search_name }}'].id"
  debug:
    msg: "{{ awx_type_id }}"
  delegate_to: 127.0.0.1
  become: false
  when:
   - debug
