ServiceNowITOM / ansible-sn-inventory

Ansible Dynamic Inventory for ServiceNow
Apache License 2.0
79 stars 45 forks source link

Possibility to filter ServiceNOW Query #5

Closed metabsd closed 5 years ago

metabsd commented 7 years ago

As an example. I want to return only the entry with status installed or in use.

Like that : https://myinstance.service-now.com/api/now/table/cmdb_ci_server?sysparm_query=install_statusIN1,101

metabsd commented 7 years ago

I also change that for the moment

options = "?sysparm_exclude_reference_link=true&sysparm_display_value=true&sysparm_query=install_statusIN1,101"

amittell commented 7 years ago

Instead of making the inventory script more complex again I think this would be better solved on the playbook side. You would set fields = install_status in your config, then in your playbook test for the value of the variable install_status as a playbook conditional.

tasks:
  - name: "Showdown Installed or In Use Serrvers"
    command: /sbin/shutdown -t now
    when: installed_status == "1" or installed_status == "101"

You could of course add any vars you need for your conditionals using the fields setting in the config.

Ansible document reference: http://docs.ansible.com/ansible/latest/playbooks_conditionals.html

metabsd commented 7 years ago

If I understand.

I have to use like that ?

tasks:
  - name: "Showdown Installed or In Use Servers"
    command: /sbin/shutdown -t now
    when: sn_install_status == "Installed" or sn_install_status == "En utilisation"
      "ourservername": {
        "sn_name": "ourservername",
        "sn_ip_address": "ourip",
        "sn_host_name": "ourservername",
        "sn_sys_class_name": "Linux Server",
        "sn_install_status": "En utilisation",
        "sn_fqdn": "ourservername.ourdomain",
        "sn_os": "Linux Red Hat",
        "sn_used_for": "Development"
      },
      "ourservername": {
        "sn_name": "ourservername",
        "sn_ip_address": "ourip",
        "sn_host_name": "ourservername",
        "sn_sys_class_name": "Linux Server",
        "sn_install_status": "Installed",
        "sn_fqdn": "ourservername.ourdomain",
        "sn_os": "Linux Red Hat",
        "sn_used_for": "Development"
      },

Thank you!!!

restump commented 7 years ago

Yup, conditionals in the tasks and host patterns should give you a lot of flexibility in targeting various hosts.

metabsd commented 7 years ago

How I can filter before Gathering Facts. Thank you!

when: sn_install_status == "Installed" or sn_install_status == "En utilisation"

ursetto commented 5 years ago

As an actual use case for this, when we have large numbers of servers, we may wish to filter sys_class_name on the server side so we can avoid downloading those server types we don't care about, such as Windows. For example, if you have 30,000 Windows servers and 10,000 Linux/Unix/Solaris servers, and Windows is out of scope, it is slow and bandwidth intensive to pull in Windows info just to discard it. Assume each type is in a separate table, so we can't just query one unix table.

Adding a query parameter -- maybe like "query = sys_class_nameINcmdb_ci_linux_server%2Ccmdb_ci_unix_server" -- would be a quick and dirty way to address this. I am going to do this in my copy, since I do need it for the reason above. It's ugly and of limited use, but if you want a PR for it, let me know.

amittell commented 5 years ago

Fixed by #23