fortinet-ansible-dev / ansible-galaxy-fortios-collection

GNU General Public License v3.0
84 stars 48 forks source link

API based auth failed: wrong credentials #335

Closed corpslave69 closed 3 weeks ago

corpslave69 commented 1 month ago

I am able to get the result when I do a normal REST API call with:

curl --location 'https://10.1.2.80/api/v2/cmdb/firewall/address' --header 'Accept: application/json' --header 'Authorization: Bearer <API_token>'

But when I try to do this over an Ansible playbook, I get this:

ansible.module_utils.connection.ConnectionError: API based auth failed: wrong credentials. Please check

I am using this playbook: fortigate.yml:

---
- name: test
  hosts: fortigate03
  connection: httpapi
  collections:
  - fortinet.fortios
  vars:
    vdom: "root"
    ansible_httpapi_use_ssl: false
    ansible_httpapi_validate_certs: false
    ansible_httpapi_port: 443
  tasks:
    - name: fact gathering
      fortinet.fortios.fortios_configuration_fact:
        vdom: root
        filters:
            - name==port1
            - vlanid==0
        sorters:
            - name,vlanid
            - management-ip
        formatters:
            - name
            - management-ip
            - vlanid
        selector: 'system_interface'

With this inventory:

[fortigates]
fortigate03 ansible_host=10.1.2.80 fortios_access_token=<API_token>

[fortigates:vars]
ansible_network_os=fortinet.fortios.fortios

I have also tried using username and password. Even that produces the same result. Why does it not work only when I'm using Ansible?

MaxxLiu22 commented 1 month ago

Hi @corpslave69 ,

Thank you for bringing this issue to our attention. I am only able to reproduce this error when I provide incorrect user/password credentials. The access token needs to be declared in tasks, but if I provide the wrong credentials, it raises Error: Invalid access token. Please check.

Could you kindly let me know if there are any special characters in your username or password? Additionally, could you please provide the details of your environment, such as the versions of ansible.netcommon and fortinet.fortios, by typing ansible-galaxy collection list these may be helpful for me to reproduce your problem.

  tasks:
    - name: fact gathering
      fortinet.fortios.fortios_configuration_fact:
        access_token: qbb4sdfczQgxmmH7km4c5qnnsw816t2
        vdom: root
        filters:
            - name==port1
            - vlanid==0
        sorters:
            - name,vlanid
            - management-ip
        formatters:
            - name
            - management-ip
            - vlanid
        selector: 'system_interface'

Thanks, Maxx