---
- name: Add hosts from CSV to Tower groups
  uri:
    method           : POST
    force_basic_auth : yes
    url_username     : "{{ __tower_user}}"
    url_password     : "{{ __tower_pass}}"
    url              : "{{ __tower_url }}/api/v2/groups/\
                        {{ r_inv_group_list | selectattr('name', '==', item.tower_group) | map(attribute='id') | flatten | join(',') }}\
                        /hosts/"
    status_code      :
      - 200
      - 201
      - 204
    headers:
      Content-type   : application/json
    body:
      name           : "{{ item.inventory_hostname }}"
      description    : "{{ item.inventory_name }}"
      inventory      : "{{ r_inv_id.id | default(r_inventory.json.id) }}"
    body_format      : json
    return_content   : no
    validate_certs   : no
  ignore_errors      : yes
  register           : r_inventory_host
  loop               : "{{ r_csv_hosts }}"
  loop_control       :
    label            : "{{ item.inventory_hostname }}"
  when               :
    - r_twr_host_list[item.inventory_hostname] is not defined


...
