pharriso / ansible_constructed_inventory

0 stars 0 forks source link

Source script fails to retrieve credentials values #1

Open samirakhelifi opened 1 year ago

samirakhelifi commented 1 year ago

Hello Mr.Harrison, first of all thank you for your blog "Ansible Constructed Inventory Plugin". Actually, i wanted to create dynamic inventory (that will contain dynamic groups) from GLPI so i used two plugins: "Ansible dynamic inventory for GLPI" customized plugin to retrieve infomation from GLPI and constructed plugin to create dynamic groups as you recommended, it does work properly in AWX but when i tried to use AWX credential management i'm always having the error indicating that he couldn't parse the variables:

Input configuration:

fields:
  - id: url
    type: string
    label: url
  - id: apptoken
    type: string
    label: apptoken
    secret: true
  - id: usertoken
    type: string
    label: usertoken
    secret: true
required:
  - apptoken
  - usertoken
  - url

Injector configuration:

env:
    GLPI_URL: '{{ url }}'
    GLPI_APPTOKEN: '{{ apptoken }}'
    GLPI_USERTOKEN: '{{ usertoken }}'

source file glpi.yml:

plugin: unistra.glpi.inv
glpi_url: "{{ GLPI_URL }}"
glpi_apptoken:  "{{ GLPI_APPTOKEN }}"
glpi_usertoken: "{{ GLPI_USERTOKEN }}"
glpi_verify_certs: False
glpi_use_headers: True
queries:
  #
  # Servers by system
  #
  glpi-non-prod-all-hosts:
    itemtype: Computer
    fields:
    - 1   # name
    - 4   # ComputerType.name
    - 31  # State.completename
    criteria:
    - { link: AND, field: 31, searchtype: contains, value: '^EN SERVICE$' }
    hostname: $1
    hostvars:
      computertype: $4
      state: $31

and i've created credentails and put the values as shown:

glpi_credentials

and i've configured the source with the target credentials:

source credentials

but i'm always getting this error message :

source-error

Also when i tried to remove these three lines from my source script as you recommended :

<glpi_url: "{{ GLPI_URL }}" glpi_apptoken: "{{ GLPI_APPTOKEN }}" glpi_usertoken: "{{ GLPI_USERTOKEN }}" >

=> I'm almost getting the same error that he can't find glpi access details.

pharriso commented 1 year ago

Hi @samirakhelifi - your setup looks good. I definitely would remove these lines from your configuration because we can't pass ansible variables to an inventory plugin.

glpi_url: "{{ GLPI_URL }}"
glpi_apptoken:  "{{ GLPI_APPTOKEN }}"
glpi_usertoken: "{{ GLPI_USERTOKEN }}"

What happens when you set those environment variables from a command line and run the inventory plugin from the command line? e.g.

export GLPI_URL=<URL>
export GLPI_APPTOKEN=<TOKEN>
export GLPI_USERTOKEN=<USER_TOKEN>

ansible-inventory -i glpi.yml --list