Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors using json_query with contains #2468

Closed
1 task done
techBeck03 opened this issue May 7, 2021 · 3 comments
Closed
1 task done

Errors using json_query with contains #2468

techBeck03 opened this issue May 7, 2021 · 3 comments
Labels
bug This issue/PR relates to a bug needs_triage plugins plugin (any type) python3

Comments

@techBeck03
Copy link

techBeck03 commented May 7, 2021

Summary

I'm trying to use the json_query plugin to filter a json object but keep getting the same error message. I've tried using the workaround mentioned in #320 but still get the same error.

Issue Type

Bug Report

Component Name

json_query

Ansible Version

$ ansible --version
ansible 2.10.9
  config file = None
  configured module search path = ['/Users/robbeck/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/robbeck/.pyenv/versions/3.9.4/envs/imm/lib/python3.9/site-packages/ansible
  executable location = /Users/robbeck/.pyenv/versions/imm/bin/ansible
  python version = 3.9.4 (default, Apr 14 2021, 15:37:58) [Clang 12.0.0 (clang-1200.0.32.29)]

Configuration

$ ansible-config dump --only-changed

OS / Environment

MacOS BigSur 11.2.3

Steps to Reproduce

- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    vms: |
      {
        "vms": {
          "json": {
            "resources": [
              {
                "ips": [
                  "192.168.1.10",
                  "10.0.0.10"
                ],
                "name": "vm-001"
              },
              {
                "ips": [
                  "192.168.1.11",
                  "10.0.0.11"
                ],
                "name": "vm-002"
              },
              ...
            ]
          }
        }
      }
  tasks:
    - set_fact:
        my_vm: "{{ vms | to_json | from_json | json_query(query) }}"
        vars:
          query: "json.resources[?ips.contains(@, '192.168.1.11')].name"

    - debug: var=my_vm

Expected Results

i should get an output containing [“vm-002”]

Actual Results

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ****************************************************************************************************************************************

TASK [set_fact] *****************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Error in jmespath.search in json_query filter plugin:\n'method' object is not iterable"}

PLAY RECAP **********************************************************************************************************************************************
localhost

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibullbot
Copy link
Collaborator

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibullbot ansibullbot added affects_2.10 bug This issue/PR relates to a bug needs_triage plugins plugin (any type) python3 labels May 7, 2021
@Ajpantuso
Copy link
Collaborator

I would check your indentation and query.

   hosts: localhost
   vars:
     vms: {"vms": {"json": {"resources": [{"ips": ["192.168.1.10", "10.0.0.10"], "name": "vm-001"}, {"ips": ["192.168.1.11", "10.0.0.11"], "name": "vm-002"}]}}}
   tasks:
     - set_fact:
         my_vm: "{{ vms | json_query(query) }}" 
       vars: # vars should be at the same indentation as the task not the task options
         query: vms.json.resources[?ips.contains(@, `192.168.1.11`)].name # Note the top-level object 'vms' and the backticks around the literal value

     - debug:
         msg: "{{ my_vm }}"

Output:

TASK [set_fact] *************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ****************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "vm-002"
    ]
}

@Akasurde
Copy link
Member

Akasurde commented May 8, 2021

Hi!

Thanks very much for your interest in Ansible. It sincerely means a lot to us.

This appears to be a user question, and we'd like to direct these kinds of things to either the mailing list or the IRC channel.

If you can stop by there, we'd appreciate it. This allows us to keep the issue tracker for bugs, pull requests, RFEs, and the like.

Thank you once again and we look forward to seeing you on the list or IRC. Thanks!

@Akasurde Akasurde closed this as completed May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug needs_triage plugins plugin (any type) python3
Projects
None yet
Development

No branches or pull requests

4 participants