"The task includes an option with an undefined variable. The error was: list object has no element 0

Viewed 22

Getting following error with Ansible playbook. Please help in finding the root cause.

Error:

TASK [CrowdStrike Falcon | Download Falcon Sensor Installation Package] ************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 0\n\nThe error appears to be in '/var/log/download3.yml': line 131, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: CrowdStrike Falcon | Download Falcon Sensor Installation Package\n      ^ here\n"}

Ansible playbook (if anyone want to test then please add falcon_client_id and falcon_client_secret before testing).

---
- hosts: localhost

  vars:

    falcon_api_url1: "https://api.us-2.crowdstrike.com"
    ##falcon_cloud: "api.crowdstrike.com"
    falcon_cloud: "api.us-2.crowdstrike.com"
    falcon_oauth2_token_url: "https://api.us-2.crowdstrike.com/oauth2/token"
    falcon_os_family: "{{ ansible_system | lower }}"
    falcon_target_os: "{{ ansible_distribution }}"
    falcon_os_version: "{{ ansible_distribution_major_version }}"
    falcon_install_tmp_dir: "/tmp"
    falcon_install_temp_directory: "/var/deploy/roles/crowdstrike/tasks"
    falcon_client_id: ""
    falcon_client_secret: ""
    falcon_sensor_update_policy_name: ""
    falcon_sensor_version_decrement: 0
    falcon_sensor_version: ""

  tasks:

    - name: Set fact for api url
      set_fact:
        falcon_api_url: "{{ falcon_api_url1 }}"
        falcon_oauth2_token_url: "{{ falcon_oauth2_token_url }}"
        falcon_os_family: "{{ falcon_os_family }}"
        falcon_target_os: "{{ falcon_target_os }}"
        falcon_install_tmp_dir: "{{ falcon_install_tmp_dir }}"
        falcon_client_id: "{{ falcon_client_id }}"

    - name: Show Fact
      debug:
        var: hostvars[inventory_hostname]

    - name: CROWDSTRIKE - AUTHENTICATE API
      ansible.builtin.uri:
        url: "{{ falcon_oauth2_token_url | d(falcon_api_url + '/oauth2/token') }}"
        return_content: yes
        method: POST
        body_format: form-urlencoded
        status_code: 201
        body:
          client_id: "{{ falcon_client_id }}"
          client_secret: "{{ falcon_client_secret }}"
      register: oauth_request

    ##- name: CrowdStrike Falcon | Get list of installers
    ##  uri:
    ##    url: "https://{{ falcon_api_url }}/sensors/combined/installers/v1?filter=platform%3A%22{{ falcon_os_family }}*%22%2Bos%3A%22{{ falcon_target_os }}%22&=&sort=version.desc"
        ##method: GET
        ##return_content: true
        ##headers:
          ##authorization: "Bearer {{ falcon_oauth2_token_url.json.access_token }}"
      ##register: falcon_api_installer_list

    - name: CrowdStrike Falcon | Filter to installers for OS major version
      set_fact:
        falcon_api_sha_hash: "{{ falcon_api_installer_list.json.resources | selectattr('os_version', 'equalto', ansible_distribution_major_version | list ) }}"

    - name: "CrowdStrike Falcon | Default Operating System configuration"
      ansible.builtin.set_fact:
        falcon_target_os: "{{ ansible_distribution }}"
        falcon_os_family: "{{ ansible_system | lower }}"
        falcon_os_version: "{{ ansible_distribution_major_version }}"
        falcon_sensor_update_policy_platform: "{{ ansible_system }}"
        falcon_os_vendor: "{{ ansible_os_family | lower if (ansible_os_family == 'RedHat' and ansible_distribution != 'Amazon') else ansible_distribution | lower }}"

    # Block when falcon_sensor_update_policy_name is supplied
    - name: Sensor Update Policy Block
      block:
        - name: "CrowdStrike Falcon | Build Sensor Update Policy API Query"
          ansible.builtin.set_fact:
            falcon_sensor_update_policy_query: "{{ 'platform_name:\"' + falcon_sensor_update_policy_platform + '\"+name.raw:\"' + falcon_sensor_update_policy_name + '\"' }}"

    - name: CrowdStrike Falcon | Authenticate to CrowdStrike API
      ansible.builtin.uri:
        url: "https://{{ falcon_cloud }}/oauth2/token"
        method: POST
        body_format: json
        body:
          "client_id={{ falcon_client_id }}&client_secret={{ falcon_client_secret }}"
        return_content: true
        follow_redirects: all
        status_code: 201
        headers:
          content-type: application/x-www-form-urlencoded
      register: falcon_api_oauth2_token
      ##no_log: "{{ falcon_api_enable_no_log }}"

    - name: Show fact falcon_api_oauth2_token
      debug:
        var: falcon_api_oauth2_token

    - name: "CrowdStrike Falcon | Build Sensor Update Policy API Query"
      ansible.builtin.set_fact:
        falcon_sensor_update_policy_query: "{{ 'platform_name:\"' + falcon_sensor_update_policy_platform + '\"+name.raw:\"' + falcon_sensor_update_policy_name + '\"' }}"

    ##- name: "CrowdStrike Falcon | Build API Sensor Query based on Sensor Update Policy"
      ##ansible.builtin.set_fact:
        ##falcon_os_query: "{{ 'os:\"' + falcon_target_os + '\"+os_version:\"' + falcon_os_version + '\"+version:\"' }}"

    - name: "CrowdStrike Falcon | Build API Query"
      set_fact:
        falcon_os_query: "{{ 'os:\"' + falcon_target_os + '\"+os_version:\"' + falcon_os_version + '\"' }}"

    - name: CrowdStrike Falcon | Get list of filtered Falcon sensors
      uri:
        url: "https://{{ falcon_cloud }}/sensors/combined/installers/v1?filter={{ falcon_os_query | urlencode }}"
        method: GET
        return_content: true
        headers:
          authorization: "Bearer {{ falcon_api_oauth2_token.json.access_token }}"
          Content-Type: application/json
      register: falcon_api_installer_list

    - name: Show Fact
      debug:
        var: falcon_api_installer_list

    - name: CrowdStrike Falcon | Filter to installers for OS major version
      set_fact:
        falcon_api_sha_hash: "{{ falcon_api_installer_list.json.resources | selectattr('os_version', 'equalto', ansible_distribution_major_version ) }}"

    - name: Show Fact
      debug:
        var: falcon_api_installer_list.json.resources

    - name: CrowdStrike Falcon | Download Falcon Sensor Installation Package
      ansible.builtin.get_url:
        url: "https://{{ falcon_cloud }}/sensors/entities/download-installer/v1?id={{ falcon_api_installer_list.json.resources[falcon_sensor_version_decrement | int].sha256 }}"
        dest: "{{ falcon_install_temp_directory.path }}"
        checksum: "sha256:{{ falcon_api_installer_list.json.resources[falcon_sensor_version_decrement | int].sha256 }}"
        mode: 0640
        headers:
          authorization: "Bearer {{ falcon_api_oauth2_token.json.access_token }}"
      changed_when: false
      register: falcon_sensor_download
      ##no_log: "{{ falcon_api_enable_no_log }}"

This is my first first post. Please bear with me.

0 Answers
Related